Implement Hill sorting by python programming and python Hill sorting

Source: Internet
Author: User

Implement Hill sorting by python programming and python Hill sorting

Observe "insert sort": in fact, it is not difficult to find that she has a disadvantage:

If the data is "5, 4, 3, 2, 1", we insert the records in the "unordered block" into the "ordered block", it is estimated that the records will crash, each insert operation must move its position. The efficiency of insertion sorting can be imagined.

Shell improved the algorithm based on this weakness and integrated the idea of "downgrading incremental sorting". It is actually quite simple, but it should be noted that:

The increment is not random, but regular.

It is difficult to analyze the sort timeliness of Hill. The comparison times of key codes and the number of records moving depend on the selection of incremental factor sequence d, you can accurately estimate the number of comparison times and the number of movements of records for a specific case. No one has provided a method to select the best incremental factor sequence. Incremental factor sequences can have various methods, including odd and prime numbers. However, note that there are no common factors except 1 in incremental factors, the last increment factor must be 1. The hill sorting method is an unstable sorting method.

First, you need to clarify the incremental method (here, the image is copied from others' blogs, the increment is odd, and the programming below uses an even number ):

The first incremental method is d = count/2;

The method for obtaining the second increment is: d = (count/2)/2;

Last until: d = 1;

Well, pay attention to the figure. In the first incremental d1 = 5, 10 records to be sorted are divided into 5 sub-sequences for direct insertion and sorting respectively. The result is (13, 27, 49, 55, 04, 49, 38, 65, 97, 76)

Incremental d2 = 3 in the second round. The 10 records to be sorted are divided into three subsequences for direct insertion and sorting respectively. The results are (13, 04, 49, 38, 27, 49, 55, 65, 97, 76)

Incremental d3 = 1 in the third round, which directly inserts and sorts the entire sequence,Final ResultIs (04, 13, 27, 38, 49, 49, 55, 65, 76, 97)

The point is. When the increment is reduced to 1, the sequence is basically ordered, and the last hop of the hill sort is the insert sort that is close to the best condition. You can consider the previous "macro" adjustment as the preprocessing of the last trip, which is more efficient than simply inserting and sorting once.

I learned python. Today I use python to implement the hill sorting.

Def ShellInsetSort (array, len_array, dk): # Insert the sort directly for I in range (dk, len_array ): # sort by position = I current_val = array [position] # index = I j = int (index/dk) # index and dk operator index = index-j * dk # while True: # Find the first subscript, In the increment of dk, the first subscript index must be 0 <= index <dk # index = index-dk # if 0 <= index and index <dk: # break # position> index, the subscript of the number to be inserted must be greater than the first subscript while position> index and current_val <array [position-dk]: array [position] = array [position-dk] # Move position later = position-dk else: array [position] = current_valdef ShellSort (array, len_array ): # Hill sort dk = int (len_array/2) # incremental while (dk> = 1): ShellInsetSort (array, len_array, dk) print (">:", array) dk = int (dk/2) if _ name _ = "_ main _": array = [49, 38, 65, 97, 76, 13, 27, 49, 55, 4] print (">:", array) ShellSort (array, len (array ))

Output:

>: [49, 38, 65, 97, 76, 13, 27, 49, 55, 4]
>>: [13, 27, 49, 55, 4, 49, 38, 65, 97, 76]
>>: [4, 27, 13, 49, 38, 55, 49, 65, 97, 76]
>>: [4, 13, 27, 38, 49, 49, 55, 65, 76, 97]

First, you have to insert the sorting, but you will not understand it.

Insert sorting is to sort the numbers in the three yellow boxes. Example :,

You can directly view 55, 55 <13 without moving. Next, let's take a look at <55, and then move 55 to [,]. Then compare the data with 13 <38, and replace 38 with 55 to [,]. Similarly, omitted.

There is a problem here, such as the second yellow box [27,4, 65], 4 <27, that 27 move back, then 4 Replace the first one, the data becomes [, 65], but how do computers know that 4 is the first one ??

In my practice, first find the subscript of the first number in [, 4, 65]. In this example, the subscript of 27 is 1. When the subscript of the number to be inserted is greater than the first subscript 1, it can be moved back,There are two situations where the previous number cannot be moved back.One is that there is data in front and it is smaller than the number to be inserted, you can only insert it behind it. The other one is very important. When the insert number is smaller than the previous number, the insert number must be placed at the first one. At this time, the subscript of the insert number = the subscript of the first number. (In this section, I feel that Beginners should not understand much ......)

To find the subscript of the first number, we first thought of using a loop Until the beginning:

While True: # Find the first subscript, In the increment of dk, the first subscript index must be 0 <= index <dk index = index-dk if 0 <= index and index <dk: break

In Debug, it is too time-consuming to use a loop. Especially when incremental d is set to 1, insert the sort directly to insert the last number of the List, and reduce the number by 1, after the subscript of the first number, I learned how to use the following method:

J = int (index/dk) # index and dk operator index = index-j * dk

Time Complexity:

The time complexity of hill sorting is the function of incremental sequence, which is difficult to analyze accurately. Some documents have pointed out that when the incremental sequence is d [k] = 2 ^ (t-k + 1), the time complexity of hill sorting is O (n ^ 1.5 ), t indicates the number of sorted shards.

Stability: unstable

Hill sorting effect:

 

Reference: programming is implemented by myself. Debug is recommended to check the running process.

Eight sorting algorithms in c ++

Visual Perception of several common sorting algorithms

C # Seven classical sorting algorithm series (below)

1. Non-systematic learning is also a waste of time 2. Be a technician who will appreciate the beauty, understand the art, and be artistic

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.