Sort four-Hill sort

Source: Internet
Author: User

Key points

The Hill (Shell) sort is also known as narrowing the incremental sort , which is an insertion sort . It is a powerful version of the direct insertion sorting algorithm .

The method is due to DL. The shell was named after it was introduced in 1959.
Hill sort of Basic IdeasIs:
The record is pressed to the mark of a certain Delta GapGroups, with each group of records Direct Insert Sortmethod to sort.
With gradual decrease in increments, the grouped groups contain more and more records, and the value to increment is reduced to 1, the entire data is combined into a group that forms an ordered set of records, and the sort is completed.

Let's take a more in-depth look at the process through a demo diagram.

In the picture above:

Initially, there is an unordered sequence of size 10.

In the first order , we might as well set gap1 = N/2 = 5, which is a group of elements separated by 5, which can be divided into 5 groups.

Next, sort each group by the method of direct insert sorting.

In the second order , we narrowed the last gap by half, i.e. GAP2 = GAP1/2 = 2 (Take an integer). This makes up a group of 2 elements per distance and can be divided into 2 groups.

Each group is sorted by the method of direct insert sorting.

In the Third Order , the gap is halved again, i.e. gap3 = GAP2/2 = 1. This makes a group of elements that are 1 apart, that is, only one group.

Each group is sorted by the method of direct insert sorting. At this time Sort has ended

It is important to note that there are two elements of equal value 5 and 5 in the figure. We can clearly see that during the sorting process, two element positions are exchanged .

So, the hill sort is an unstable algorithm.

Core code

PublicvoidShellsort (int[] list) {
intGap = LIST.LENGTH/2;

while(1 <= Gap) {
//group the elements from gap to scan all groups
for(inti = gap; i < list.length; i++) {
intj = 0;
inttemp = List[i];

//sorting element groups that are distance gap
for(j = i-gap; J >= 0 && temp < list[j]; j = j-gap) {
List[j + gap] = list[j];
}
List[j + gap] = temp;
}

Gap = GAP/2;//Decrease Increment
}
}

Algorithm Analysis

Algorithm performance for Hill sorting

Sort category Sorting methods Complexity of Time Complexity of space Stability Complexity
Average situation Worst case scenario Best case
Insert Sort Hill sort O (NLOG2N) O (NLOG2N)
O (1) Not stable More complex

Complexity of Time

Hill sort because it is a sort process that is continuously grouped, its number of executions can be considered as a complete binary tree height, i.e. nlog2n.

algorithm Stability

By the above Hill Sort algorithm Demo diagramThat is to be seen, the equivalent data in the hill sort may exchange positions, so the hill sort is not stableThe algorithm.

Comparison of direct insert sort and hill sort

The direct insert sort is stable , while the hill sort is unstable.

The direct insert sort is better suited to the basic ordered collection of original records.

The number of comparisons and movements of hill sort is less than that of direct insertion, and when n is larger, the effect becomes more pronounced.

In the hill sort, the incremental sequence gap must be fulfilled: the last increment must be 1 .  

Direct Insert sorting also applies to chained storage structures ; Hill sort does not apply to chained structures .


Full Reference Code Java version
Code Implementation

The initial sequence in the sample code is exactly the same as the sequence in this illustration.

1 PublicclassShellsort {
2
3 PublicvoidShellsort (int[] list) {
4intGap = LIST.LENGTH/2;
5
6 while(1 <= Gap) {
7 //group the elements from gap to scan all groups
8 for(inti = gap; i < list.length; i++) {
9intj = 0;
Teninttemp = List[i];
One
A//sorting element groups that are distance gap
- for(j = i-gap; J >= 0 && temp < list[j]; j = j-gap) {
-List[j + gap] = list[j];
the}
-List[j + gap] = temp;
-}
-
+System.out.format ("gap =%d:\t", GAP);
-Printall (list);
+Gap = GAP/2;//Decrease Increment
A }
at}
-
-//Print Complete Sequence
- PublicvoidPrintall (int[] list) {
- for(intValue:list) {
-System.out.print (value + "\ T");
in}
-System.out.println ();
to}
+
- PublicStaticvoidMain (string[] args) {
theint[] Array = {9, 1, 2, 5, 7, 4, 8, 6, 3, 5};
*
$ //Call the Hill sort method
Panax NotoginsengShellsort Shell =NewShellsort ();
-System.out.print ("Before sorting: \t\t");
theShell.printall (array);
+Shell.shellsort (array);
ASystem.out.print ("After sorting: \t\t");
theShell.printall (array);
+}
-
$}Java code Implementation of Hill sort

Run ResultsBefore sorting: 9 1 2 5 7 4 8 6 3 5
Gap = 5:4 1 2 3 5 9 8 6 5 7
Gap = 2:2 1 4 3 5 6 5 7 8 9
Gap = 1:1 2 3 4 5 5 6 7 8 9
After sorting: 1 2 3 4 5 5 6 7 8 9

References "Data structure exercises and analysis" (B-Level 3rd edition)

Sort four-Hill sort

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.