Created
August 8, 2018 18:45
-
-
Save TomColBee/a65f355fec2fd73146608801796555c3 to your computer and use it in GitHub Desktop.
Bubble Sort Algorithm
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(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