Hill Sort:
Take a look at "insert sort": It's not hard to see that she has a flaw:
If the data is "5, 4, 3, 2, 1" When the record in the "unordered block" is inserted into the "ordered block", we are expected to collapse,
Each time the insertion is moved, the efficiency of the insertion order can be imagined.
The shell has improved the algorithm based on this weakness, incorporating a thought called "Narrowing incremental sorting", which is actually quite simple, but a little bit of attention is:
An increment is not a random, but a rule to follow.
It is difficult for hill to sort the time-lapse analysis, the comparison number of key code and the number of record moves depend on the selection of increment factor sequence D , which can accurately estimate the comparison number of key codes and the number of movement of records in certain cases. At present, no one has given a method to select the best increment factor sequence. increment factor sequence can have a variety of ways, there are odd, but also take prime numbers , but need to note: The increment factor in addition to 1 there is no public factor, and the last increment factor must be 1. The hill sort method is an unstable sort method.
First of all to clarify the increment of the extraction (here the picture is copy other people's blog, the increment is odd, I use the following programming is even):
The first increment is: D=COUNT/2;
The second increment is: d= (COUNT/2)/2;
Last until: d=1;
OK, take a look at the figure, the first trip of the increment d1=5, the 10 backlog records are divided into 5 sub-sequences, respectively, the direct insertion of the order, The result is (55, 04, 49, 38, 65, 97, 76)
The second trip increment d2=3, divides 10 backlog records into 3 sub-sequences, respectively carries on the direct insertion sort, the result is (38, 27 , 49, 55, 65, 97, 76)
Incremental d3=1 of the third trip, direct insertion of the entire sequence, with the final result (49, 55, 65, 76 , 97)
The point has come. When the increment is reduced to 1 o'clock, the sequence is basically orderly, and the last trip to the hill sort is the direct insertion sort that approximates the best case . The "macro" adjustment of the previous trips can be regarded as the preprocessing of the last trip, which is more efficient than only one direct insertion.
I was learning python, and today Python implements the hill sort.
1 defShellinsetsort (Array, Len_array, DK):#Direct Insert Sort2 forIinchRange (DK, Len_array):#Insert sort from the number subscript DK3Position =I4Current_val = Array[position]#the number to insert5 6index =I7j = Int (INDEX/DK)#the quotient of index and DK8index = Index-j *DK9 Ten #while True: # finds the first subscript, in increments of DK, the first subscript index must 0<=INDEX<DK One #index = INDEX-DK A #if 0<=index and index <DK: - # Break - the - #Position>index, the subscript of the number to be inserted must be greater than the first subscript - whilePosition > Index andCurrent_val < array[position-DK]: -Array[position] = Array[position-dk]#move backwards . +Position = position-DK - Else: +Array[position] =Current_val A at - - defShellsort (Array, Len_array):#Hill Sort -DK = Int (LEN_ARRAY/2)#Incremental - while(DK >= 1): - shellinsetsort (Array, Len_array, DK) in Print(">>:", Array) -DK = Int (DK/2) to + if __name__=="__main__": -Array = [49, 38, 65, 97, 76, 13, 27, 49, 55, 4] the Print(":", Array) *Shellsort (array, len (array))
Output:
(a): [4, A, 4, $,------------------------]>> [4, +,------------]>>: [4, 13, 27, 38, 49, 49, 55, 65, 76, 97]
First you have to insert the sort first, not necessarily you can not understand.
Insert sort, which is the number of three yellow boxes in the insertion sort. As an example: 13,55,38,76
Look directly at the 55,55<13 without moving. Then look at 38,38<55, then 55, then the data becomes [13,55,55,76], then the comparison 13<38, then 38 replaces 55, becomes [13,38,55,76]. Other similar, slightly.
Here is a question, such as the second yellow box [27,4,65],4<27, the 27 moves backwards, then 4 replaces the first, the data becomes [4,27,65], but how does the computer know that 4 is the first one??
My approach is to find the subscript for the first number of [27,4,65], in thiscase the subscript of 27 is 1. When the subscript of the number to be inserted is greater than the first subscript 1 o'clock, you can move backward, the previous number can not be moved backwards there are two cases , one is the previous data, and less than the number to be inserted, then you can only plug behind it. Another, it is important, when you want to insert more than the number of hours before, the insertion number is definitely placed in the first, at this point to insert the number of subscript = The first number of subscript. (This passage, feel beginners should not understand ...)
In order to find the first number of subscripts, the beginning is to use the loop, all the way to the front:
While True: # finds the first subscript, in increments of DK, the first subscript index must 0<=INDEX<DK index = index-dk if 0<=index and Index & LT;DK: Break
In debug, it is found that using loops is a waste of time, especially when the increment d=1, the direct insertion sort in order to insert the last number of the list, the loop minus 1 until the first number of subscript, and then I learned smart, with the following method:
j = Int (INDEX/DK) # Index and DK quotient index = index-j * DK
Complexity of Time:
The time complexity of hill sorting is the function of the increment sequence, which is difficult to analyze accurately. It is pointed out that when the increment sequence is d[k]=2^ (t-k+1), the time complexity of the hill sort is O (n^1.5), where T is the number of sort trips.
Stability: Unstable
Hill Sort Effect:
Reference: Programming is my own implementation. Suggest debug to see the running process
Http://blog.csdn.net/hguisu/article/details/7776068#t2
Http://www.blogjava.net/todayx-org/archive/2012/01/08/368091.html
Http://www.cnblogs.com/huangxincheng/archive/2011/11/20/2255695.html
Python Implements Hill sort (implemented programmatically)