Created
August 12, 2020 12:19
-
-
Save Neptune998/9b1fd070e878ec24f77ea4fe29d0fdf7 to your computer and use it in GitHub Desktop.
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(arr, itr): | |
lgth = len(arr) | |
for i in range(lgth): | |
for j in range(lgth-i-1): | |
if arr[j]>arr[j+1]: | |
arr[j],arr[j+1] = arr[j+1],arr[j] | |
print("Iteration:",i,arr) | |
itr+=1 | |
return arr, itr | |
if __name__ == '__main__': | |
arr = list(map(int, input("Enter array:").split())) | |
# arr = [5,4,3,2,1] | |
sorted_arr, itr = bubble_sort(arr, itr=0) | |
print("Total Iterations/Swaps:",itr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment