Skip to content

Instantly share code, notes, and snippets.

@gerep
Created Jan 21, 2022
Embed
What would you like to do?
Bubble sort
def bubble_sort(nums):
n = len(nums)
swap = True # required to start the while loop
while swap:
swap = False # always defaults to False
for i in range(1, n):
if nums[i-1] > nums[i]:
nums[i-1], nums[i] = nums[i], nums[i-1]
swap = True
return nums
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment