?? Python uses binary insertion sorting to be 99 times times faster than a direct insert sort!
Before publishing the same algorithm, C + + unexpectedly 8 times times faster than C! , some students suggest that because C + + uses a two-point insertion sort, so Python compares the next two sorts of differences in how big.
The test results are as follows:
Python Insertion Sort took time:1:39:42.448904
Python insertion Sort with binary search took time:0:01:13.263267
The code is as follows:
Import Datetimeimport bisectdef Insertion_sort (L): for I in xrange (1, Len (l)): j = i-1 key = L[i] while (l [j] > key) and (J >= 0): l[j+1] = l[j] J-= 1 l[j+1] = keydef insertion_sort_bin (seq): For I in rang E (1, Len (seq)): bisect.insort (seq, Seq.pop (i), 0, I) a=[]for x in range (200000): A.append (x) a.reverse () start = Datetime.datetime.now () Insertion_sort (a) end = Datetime.datetime.now () print "Python insertion sort took time:%s"% ( End-start) a.reverse () Start2 = Datetime.datetime.now () insertion_sort_bin (a) End2 = Datetime.datetime.now () print " Python insertion Sort with binary search took time:%s "% (END2-START2)
The same python, the algorithm is not the same, the speed difference hundred times!
Python uses binary insertion sorting to be 99 times times faster than a direct insert sort!