Algorithm-Interview icon indicating copy to clipboard operation
Algorithm-Interview copied to clipboard

A bug in the basic implementation

Open terizhao opened this issue 10 years ago • 5 comments

def bubble_sort(array): 17 lenth = len(array) 18 19 for i in range(lenth): 20 flag = True 21 for j in range (1, lenth): # it should be array's length other than array's length -1. 22 if (array[j-1]<array[j]): 23 array[j],array[j-1]=array[j-1],array[j] 24 flag = False 25 26 if flag: 27 break 28 print array 29 return array

terizhao avatar Jun 01 '15 06:06 terizhao

@terizhao think more,and use length-i could avoid checking part that already has been sorted

Allianzcortex avatar Mar 17 '16 08:03 Allianzcortex

@terizhao think more,and use length-i could avoid checking part that already has been sorted

Allianzcortex avatar Mar 17 '16 08:03 Allianzcortex

@terizhao think more,and use length-i could avoid checking part that already has been sorted

Allianzcortex avatar Mar 17 '16 08:03 Allianzcortex

@terizhao think more,and use length-i could avoid checking part that already has been sorted

Allianzcortex avatar Mar 17 '16 08:03 Allianzcortex

@terizhao think more,and use length-i could avoid checking part that already has been sorted

Allianzcortex avatar Mar 18 '16 10:03 Allianzcortex