Sort algorithms for algorithm learning: Hill sort, learning sort algorithm Hill

Source: Internet
Author: User

Sort algorithms for algorithm learning: Hill sort, learning sort algorithm Hill

Hill sortingAlso known as "downgrading incremental sorting", the basic idea is to divide the entire sequence of records to be sorted into several subsequences for direct insertion and sorting, respectively, when the record in the entire sequence is "basically ordered", the record is directly inserted and sorted.


A characteristic of hill sorting is that the subsequence is not simply "segmented by segments", but a subsequence is composed of records separated by a certain "increment. This makes the record with a small keyword in the hill sorting not move forward step by step, but one step forward according to the size of the "increment, in this way, the sequence is basically ordered when the last incremental step is 1, and sorting can be completed by comparing and moving a few records, therefore, the time complexity of hill sorting is lower than that of direct insertion sorting.


The following uses N = 10 records as an example to analyze the hill sorting process. The record is as follows:

49 38 65 97 76 13 27 49 55 04

For the first sort, the incremental value is Increment = N/2 = 5, so:

49 38 65 97 76 13 27 49 55 04

1A 1B

2A 2B

3A 3B

4A 4B

5A 5B

Incremental sorting = Increment/2 = 2 is selected for the second sorting. The first sorting result is as follows:

13 27 49 55 04 49 38 65 97 76

1A 1B 1C 1D 1E

2A 2B 2C 2D 2E

Incremental sorting = Increment/2 = 1 is selected for the third round of sorting. The second round of sorting results are as follows:

04 27 13 49 38 55 49 65 97 76

Incremental sorting = Increment/2 = 0 indicates that the entire sorting is completed by the third sorting. The result is as follows:

04 13 27 38 49 49 55 65 76 97


In the implementation of the hill sorting algorithm, an important issue is the selection of incremental sequences, because it is related to the performance of the hill sorting. The performance of different incremental sequences varies greatly. Generally, the first Increment is set to Increment = N/2, and the subsequent Increment is set to Increment = Increment/2;


Sample Code 1 (implemented in C ):

/*************************************** * ***************************** Author: lee ice date: Email: libing1209@126.com @ array: the pointer to the records @ num: the length of the records *********************************** * *********************************/void shellsort (int array [], int num) {if (array = NULL | num <0) return; int I, j, increment; for (increment = num/2; increment> 0; increment/= 2) // incremental sequence {// Insert the sort directly for (I = 0; I <increment; I ++) {for (j = I + increment; j <num; j + = increment) {if (array [j] <array [j-increment]) {int tmp = array [j]; int k = j-increment; while (k> = 0 & array [k]> tmp) {array [k + increment] = array [k]; k-= increment ;} array [k + increment] = tmp ;}}}}}


The hill sorting given above is completely given by definition, which is complicated. The simplified version of the hill sorting is given below.

Sample Code 2 (implemented in C ):

/*************************************** * ***************************** Author: lee ice date: Email: libing1209@126.com @ array: the pointer to the records @ num: the length of the records *********************************** * *********************************/void shellsort (int array [], int num) {if (array = NULL | num <0) return; int I, j, increment; int tmp; for (increment = num/2; increment> 0; increment/= 2) {for (I = increment; I <num; I ++) {tmp = array [I]; for (j = I; j> = increment; j-= increment) {if (tmp> array [j-increment]) array [j] = array [j-increment]; elsebreak ;} array [j] = tmp ;}}}

Summary:

1. The Increment of hill sorting is N/2 and Increment/2.

2. The worst case of hill sorting is O (n ^ 2 ).


References:

1. Edited by Yan Weimin Wu Weidong, data structure (C language version)

2. Data Structure and algorithm analysis-C language description Mark Allen Weiss translated by Feng shunxi

3. http://blog.csdn.net/morewindows/article/details/6668714




Hill Sorting Algorithm

Sorry, you have no opinion on the English version.
Donald L. shell was born on a farm near Croswell, Michigan, on March 1, 1924. because of his aptitude for education, the day of his sixth birthday he started studying at the local school house. he progressed quickly and went to Michigan clinical University where he acquired a BS in Civil Engineering in three years.

After acquiring the BS, he went into the Army Corps of Engineers, and from there to Philippines to help repair damages during the World War II. when he returned after the war, he married Alice McCullough and returned to Michigan clinical University, where he taught mathematics. after, he moved to Cincinnati, Ohio, and worked for General Electric's engines division, where he developed a convergence algorithm and wrote a program to perform calculations for the performance cycle for aircraft jet engine. he also went to the University of Cincinnati, where in 1951 he acquired a M.S. in mathematics, and 8 years later, in 1959, he acquired his Ph. d. in Mathematics. in July the same year he published the shell sort algorithm [1] and "The Share 709 System: A Cooperative Effort ". the year before, in 1958, he and. spitzbart published "A Chebycheff Fitting Criterion ".

Although he is widely known mostly for his shell sort algorithm, his Ph. d. is also considered by some to be the first major investigation of the convergence of infinite exponentials, with some very deep results of the convergence into the complex plane. this area ...... remaining full text>

Proof of hill Sorting Algorithm

Hill sorting:
* A large amount of auxiliary space is not required, which is as easy to implement as merging and sorting. Hill sorting is an algorithm based on insert sorting,
* This algorithm adds a new feature to improve efficiency. The time complexity of hill sorting is O (N * (logN) 2 ),
* There is no fast Sorting Algorithm for fast O (N * (logN). Therefore, the medium-size and large-sized data sorting is not
* Optimal choice. But it is much faster than the O (N2) algorithm. In addition, Hill sorting is very easy to implement, and the algorithm code is short and simple.
* In addition, the efficiency of the hill algorithm in the worst case is not much different from that in the average case, while fast sorting is in the worst case.
* The execution efficiency is very poor.
*
* Experts advocate that almost any sorting work can be sorted by hill at the beginning. If it is proved that it is not fast enough in actual use,
* Change to a more advanced sorting algorithm such as quick sorting.
*
* In essence, an improvement of the hill sorting algorithm reduces the number of copies, which is much faster.
* The reason is that when the N value is large, the number of data items in each sort is very small, but the distance between data items is very long.
* When the N value is reduced by an hour, the required and dynamic data increases, which is close to the final position after their sorting.
* It is the combination of these two cases that makes Hill sorting more efficient than insert sorting.

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.