13. Toad's data structure Advanced 13 sort implementation of Hill sort method
This famous article:"a person naked to this world , finally naked left the world and go , and thoroughly think up , Fame and wealth are outside the body , only to do one's heart , so that the community of people more than the benefits of his work , is the most enjoyable thing in life. -- Zou Taofen "
Here's the hill sort method.
Welcome reprint, Reprint please indicate source: http://blog.csdn.net/notbaron/article/details/47706135
1. Hill sort
The Hill Sort (shellsort) is one of the insertion sorts . Also known as narrowing incremental sorting, is a more efficient and improved version of the direct insertion sorting algorithm. Hill Sort is a non-stable sorting algorithm. The method is due to DL. The shell was named after it was introduced in 1959.
Hill sort is to group records by a certain increment of the subscript, sorting each group using the direct insertion sorting algorithm; As the increments gradually decrease, each group contains more and more keywords, when the increment is reduced to 1 o'clock, the entire file is divided into a group, the algorithm terminates.
2. Code implementation
The incremental sequence is simply handled here: Delta sequence d = {N/2, N/4, N/8 ..... 1} n is the number of digits to be sorted
The set of records to be sorted is divided into groups of sub-sequences by an increment D (N/2,n is the number of numbers to be sorted), and the subscript difference of the records in each group D. Direct insert sorting of all elements in each group, followed by a smaller increment (D/2) to group it, followed by a direct insert sort in each group. Continue to shrink the increment until it is 1, and finally use the direct insert sort to complete the sorting.
The implementation of the code is simple, similar to direct insertion, just more incremental sequence.
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.
PS: There is no public factor except 1 in the increment factor, and the last increment factor must be 1. The hill sort method is an unstable sort method.
3. Source Code
#include "Stdio.h"
void print (inta[],intn ,inti) {
for (intj= 0; j<8; j + +) {
printf ("%d",a[j]); }
}
/**
* general form of direct insertion sort
*
* @param int DK reduce the increment, if you are inserting the sort directly, dk=1
*
*/
void Shellinsertsort (inta[],intn,int DK)
{
for (inti= dk; i<n; ++i) {
if (a [i]< a [I-DK ]) { // if section i elements greater than i-1 element, insert directly. If less, move the ordered table and insert &NBSP;
int j= i-DK;
int x= a[i]; // Copy as Sentinel, which stores the elements to be sorted
a [i]= a [I-DK ]; // First move back one element &NBSP;
while (x< a[j]) { // Find the insertion position in an ordered table
a [j+dk]=a[j];
j-=DK; // element Move back
}
a [j+dk]= x; // Insert to correct position
}
//Print (A, n,i);
}
}
/**
* first by Increment D ( N/2,n sort the number of numbers to be sorted by Hill
*
*/
void shellsort (inta[],intn) {
intdk = n/2;
while (DK >= 1) {
Shellinsertsort (a,n, DK);
Dk= DK/2;
}
}
int Main () {
intA[8] = {3,1,5,7,2,4,9,6};
//shellinsertsort (a,8,1);// Direct Insert Sort
printf ("Beforeshell insert\n");
Print (a,8,8);
Shellsort (a,8); // Hill Insert Sort
printf ("\naftershell insert\n");
Print (a,8,8);
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
13. Toad's data structure Advanced 13 sort implementation of Hill sort method