Skip to content

Instantly share code, notes, and snippets.

@TomColBee
Created August 8, 2018 18:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TomColBee/a65f355fec2fd73146608801796555c3 to your computer and use it in GitHub Desktop.
Save TomColBee/a65f355fec2fd73146608801796555c3 to your computer and use it in GitHub Desktop.
Bubble Sort Algorithm
# bubble sort
def bubble_sort(n):
moves = 0
print("Unsorted list: {}".format(unsorted_list))
length_of_list = len(unsorted_list)
print("Number of numbers : {}".format(length_of_list))
for i in range(length_of_list,0,-1):
for k in range(i-1):
if unsorted_list[k] > unsorted_list[k+1]:
saved_index_value = unsorted_list[k]
unsorted_list[k] = unsorted_list[k+1]
unsorted_list[k+1] = saved_index_value
moves += 1
print("Sorted list : {}".format(unsorted_list))
print("Number of iterations made : {}".format(moves))
unsorted_list = [5,1,3,2,6,16,17,29,0.1]
bubble_sort(unsorted_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment