Created
March 3, 2017 05:13
-
-
Save Kwisses/247d5c576edff6d08db43db831846a3e to your computer and use it in GitHub Desktop.
[Bubble Sort] - Python 3.5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bubble Sort | |
def bubble_sort(nums, step=True): | |
"""Implement the Bubble sort on nums. | |
Args: | |
nums (list): Contains integers to sort. | |
step (bool): If True, prints each iteration. | |
Returns: | |
list: Naturally-sorted nums. | |
""" | |
for a in range(len(nums) - 1): | |
if step: | |
print(nums) | |
for b in range(1, len(nums) - 1): | |
if nums[b-1] > nums[b]: | |
t = nums[b-1] | |
nums[b-1] = nums[b] | |
nums[b] = t | |
return nums | |
def main(): | |
"""Run bubble_sort() on nums.""" | |
nums = [5, 4, 7, 3, 10, -5, -7, 20] | |
nums_sorted = bubble_sort(nums) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment