Python implementation of Hill sort code example

Source: Internet
Author: User
This article mainly introduces the python implementation of Hill sort, has been programmed to implement the hill sort, with a certain reference value, interested in small partners can refer to

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 we insert the "unordered block" record into the "ordered block", we estimate that we will crash, each insertion to move the position, the efficiency of the insertion sort is conceivable.

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 sort , the result is (13, 27, 49, 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 (13, 04, 49, 38, 27, 49, 55, 65, 97, 76)

Incremental d3=1 of the third trip, direct insertion of the entire sequence, with the final result (04, 13, 27, 38, 49, 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.

def shellinsetsort (Array, Len_array, DK): # Direct Insert sort for I in range (DK, len_array): # Insert sort from subscript to DK position = i Current_va L = array[position] # number to insert index = i j = Int (INDEX/DK) # Index with DK quotient index = index-j * DK # while True: # Find the first subscript, in increments For DK, the first subscript index must 0<=INDEX<DK # index = index-dk # if 0<=index and index <DK: # break # Position>index, to  The subscript of the inserted number must be greater than the first subscript while position > Index and Current_val < ARRAY[POSITION-DK]: array[position] = array[position-dk] # move Backwards position = position-dk Else:array[position] = current_valdef shellsort (array, Len_array): # Hill Sort dk = Int (Len_arr AY/2) # Incremental while (DK >= 1): Shellinsetsort (Array, Len_array, DK) print (">>:", array) dk = Int (DK/2) If __name__ = " __main__ ": array = [+, 4, N, $, +, +, +, $,-----] Print (";: ", array) shellsort (array, len (array))

Output:

(a): [4, 4, 27, 13, 49, 38, and 4]>>: [+], +,---------------- , 4, 13, 27, 38, 49, 49, 55, 65, 76, 97, 76]>>

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 <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

Eight sorting algorithms in C + +

Some common sorting algorithms for visual intuition perception

C # Seven classic sorting algorithm series (bottom)

1. Non-systematic learning is also a waste of time 2. Be a technology that will appreciate beauty, understand art and art

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.