Created
November 4, 2020 23:26
-
-
Save richardbwest/73d4638cf0376f27609c8cc16614d45b to your computer and use it in GitHub Desktop.
Bubble Sort Algorithm Tutorial
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
import time,random | |
l = [] | |
for i in range(20): | |
l.append(random.randint(1,1000)) | |
def bubble_sort(l): | |
l = l[:] | |
while True: | |
swapped = False | |
for index in range(len(l)-1): | |
if l[index] > l[index+1]: | |
l[index], l[index+1] = l[index+1], l[index] | |
swapped = True | |
if swapped == False: | |
return(l) | |
sorted_list = bubble_sort(l) | |
print("sorted") | |
print(l) | |
print(sorted_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment