Skip to content

Instantly share code, notes, and snippets.

@mortymacs
Last active May 25, 2017 07:54
Show Gist options
  • Select an option

  • Save mortymacs/8c2967610a65e9159625628cfe7b9955 to your computer and use it in GitHub Desktop.

Select an option

Save mortymacs/8c2967610a65e9159625628cfe7b9955 to your computer and use it in GitHub Desktop.
Simple Bubble Sort in Python
def bubble_sort(data):
i, j, swap = 0, 0, 0
while i < len(data) - 1:
while j < len(data) - 1:
if data[j] > data[j+1]:
data[j], data[j+1] = data[j+1], data[j]
swap += 1
j += 1
if swap == 0: # If no change find, so array is already sorted
break
j = 0
i += 1
return data
print(bubble_sort([3,2,1]))
# [1, 2, 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment