1. Insert Sort:
Insert Sort By default the first is the list that is already sorted, after each one is compared and sorted, after the row, all the natural has been lined up, the second optimization algorithm, if it has been inserted, to find the right position, then break, if you do not need to sort, then it is already the largest, Because the front has been lined up, after the row, there is no need to row, you can break.
Import Timestart = Lambda:time.time () List1 = [66,33,1,3,111,3,10,29,7,123,56,23] * 50def busorted (unsorted): L = len (unsorted) For I in Range (0,l): key = Unsorted[i] for x in range (i,0,-1): if UNSORTED[X] < Unsorted[x-1]: unsorte D[X], unsorted[x-1] = unsorted[x-1], unsorted[x] return unsorteddef optimizesorted (unsorted): l = Len (unsorted ) for I in range (0,l): key = Unsorted[i] for x in range (i,0,-1): if unsorted[x] < UNSORTED[X-1]:
UNSORTED[X], unsorted[x-1] = unsorted[x-1], unsorted[x] else: break return unsortedif __name__ = = ' __ main__ ': print (list1) t1 = start () print (busorted (list1)) t2 = start () print ('----------- Time is {} '. Format (T2-T1))
Python algorithm Exercises