#bubble sort
#time complexity: O(n^2)
#space complexity: O(1)
a = [6,7,4,3,9,1,2]
def bubble(lst):
print(lst)
length = len(lst)
if length <=1:
return
for i in range(0,length):
for j in range(length-1,i,-1):
if lst[j] < lst[j-1]:
lst[j],lst[j-1] = lst[j-1],lst[j]
print(lst)
bubble(a)
网友评论