Shell sorting uses grouping to accelerate the ordering of partial ordered arrays, and the time performance of grouping fixed-length jumping bubble hill is better than the reason of direct insertion ordering: ① The number of comparisons and moves required to insert a sort directly when the initial state of the file is basically ordered is low. ② when the n value is small, the difference between N and n^2 is also small, that is, the best time to insert the order of the complexity O (n) and the worst time complexity of 0 () is not very different. ③ in the beginning of the hill, the increment larger, more groups, the number of records in each group is small, so the direct insertion within the group is faster, and then the incremental di gradually reduced, and the number of groups gradually decreased, and the group of records gradually increased, but because already according to Di-1 as a distance arrangement, so that the file is closer to the orderly state So the new trip sort process is also faster. Therefore, the hill sort is more efficient than the direct insertion sort.
#-*-coding:utf-8-*-defShell (arr):#recursive jump, equivalent to insert shift defStep_insert (_arr, I, h):ifI < Hor_arr[i] >= _arr[i-h]:return Else: _arr[i], _arr[i-H] = _arr[i-h], _arr[i] Step_insert (_arr, I-h, h)#Step Size defstep_list (_arr): _list=[] H= 1 whileH < Len (_arr)/3: _list.append (h) H= 3 * H + 1return_list[::-1] forHinchstep_list (arr): forIinchRange (H, Len (arr)): Step_insert (arr, I, h)
Import Random
for in range (20
Print
Print arr
Python sort-Hill sort