Skip to content

Instantly share code, notes, and snippets.

@Kwisses
Created March 3, 2017 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kwisses/247d5c576edff6d08db43db831846a3e to your computer and use it in GitHub Desktop.
Save Kwisses/247d5c576edff6d08db43db831846a3e to your computer and use it in GitHub Desktop.
[Bubble Sort] - Python 3.5
# 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