Java Learning-Insert sort 2-Hill sort

Source: Internet
Author: User

Hill Sort:

1. Basic ideas:

First take an integer less than n D1 as an increment, the number of the array is divided into D1 Group, the I+D1 number is a group, the number of each group in the direct insertion sort. Take the second number d2<d1 as the second increment, repeating the first step grouping and sorting until d= 1, that is, all records are placed in the same group for the direct insertion of the sort, the essence of the hill sort is grouping for direct insertion sort.

* * Data structure Book on the Uchille sort of method, as a basic sorting method, or to master the drop **//

2: Example


Explain: When the increment is 5 o'clock, 57 and 28 are a group, 68 and 96 are a group, 59 and 33 are a group, 52 and 24 are a group, 72 and 19 are a group;

Direct insert sorting for a single grouping, followed by the result as shown in the second row. When the second increment is 3 o'clock, 28,24,96,72 is a group, and 68,19,59 is a

Group, 33,57,52 is a group, and then it is sorted, and the result is as shown in the third row. Until the increment is 1 o'clock, it is sorted again, the result.

3: source code;

Package Xierpaixu;
public class Xierpaixu {
public static void Main (String args[]) {
Int[] array = {23,34,12,45,56,43,67,65,78,76};
System.out.println ("Before sorting:");
for (int i=0;i<array.length;i++)
System.out.print (array[i]+ "");
int d=array.length;
while (true) {
D=D/2; To group
for (int i =0;i<d;i++) {
for (int x=i+d;x<array.length;x++) {//To determine the number of i+d within the array as a group
int temp=array[x];
Int J;
for (j=x-d;j>=0&&array[j]>temp;j=j-d) {//Direct insert sort for elements within group
ARRAY[J+D] = Array[j];
}
ARRAY[J+D] = temp;
}
}
if (d = = 1) {
Break Ends when incremental d=1.
}
}
System.out.println ();
System.out.print ("After sorting:");
for (int i=0;i<array.length;i++) {
System.out.print (array[i]+ "");
}
}
}

4. Analysis

We know that once the insertion sort is stable, but in different insertion sorts, the same elements may move in their own insert sort, and finally their stability will be disturbed, so the hill sort is unstable.

The time performance of the Hill sort is better than the direct insert sort for the following reasons:

(1) The number of comparisons and movements required to insert a sort directly when the initial state of the file is basically ordered is less. (2) when the n value is small, the difference between N and N2 is also small, that is, the best time to insert the order of the complexity O (n) and the worst time complexity 0 (n2) difference is not small. (3) in the beginning of the hill, the increment is larger, more groups, the number of records per group is small, so the direct insertion within the group is faster, the increment di gradually reduced, the number of groups gradually decreased, and the number of records of the group gradually increased, but because already according to Di-1 as a distance arrangement, so that the file is closer to the So the new trip sort process is also faster. Therefore, the efficiency of hill sort is better than direct interpolation. The average time complexity for hill sorting is O (Nlogn).
//Analysis is what I see on other blogs, feeling analysis is very thorough, can learn from learning


Java Learning-Insert sort 2-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.