An example of hill sorting using Java _java

Source: Internet
Author: User
I. Theoretical preparation
Hill sort (Shell sort) is a sort of insertion, which is for the improvement of the direct insertion sort algorithm, which is to divide the whole sequence into several small sequences to insert the sequence separately, and the hill sort is not stable. The method is also called narrowing the incremental order, due to DL. The shell was named after it was introduced in 1959.
Basic idea: First take an integer D1 less than n as the first increment, the entire record of the file into D1 groups. All records with a multiple distance of D1 are placed in the same group. A direct insertion sort is performed within each group, and then the second increment d2<d1 repeats the grouping and sorting above until the incremental dt=1 (DT&LT;DT-L&LT;...&LT;D2&LT;D1) is taken, where all records are placed in the same group for direct insertion sort.
The time performance of hill sorting is better than the reason for direct insertion sort:
① The comparison and the number of moves required to insert a sort directly when the file's initial state is basically orderly.
② when the n value is small, the difference between N and N2 is also small, that is, the best time complexity O (n) and the worst time complexity 0 (N2) are not significantly different in the direct insertion order.
③ at the start of Hill sort, the increment is larger, group more, the number of records per group is small, so the direct insertion of each group is faster, then the incremental di gradually narrowed, the number of groups gradually decreased, and the number of records of each group gradually increased, but because the di-1 as a distance to order, so that the file is close to the orderly State, So the new sequencing process is also quicker.
Therefore, Hill sort has a great improvement in efficiency than direct interpolation.
Selection of the increment sequence: The execution time of the shell sort depends on the increment sequence.
The common features of a good incremental sequence (all the information that is found):
① the last increment must be 1;
② should try to avoid the case that the values in the sequence (especially the adjacent values) are multiples of each other.
See this, I want to try Hill sort, learn.
Copy Code code as follows:

public class Shellsort {
public static void Main (string[] args) {

int[] arr = new int[]{44,33,99,10,30,20,59,78,23,48};
System.out.print ("Sort before:");
for (int o:arr) {
System.out.print (o+ "");
}
System.out.println ();
Shellsort (arr);
System.out.print ("after sort:");
for (int o:arr) {
System.out.print (o+ "");
}
System.out.println ();
}
private static void Shellsort (int[] arr) {

Int J;
int len = arr.length;
for (int val=len>>1; val>0; val>>=1) {
The following is a direct insert sort for all the groups in this time
for (int i=val; i<len; i++) {
int temp = Arr[i];
/*
* Why do you use temp comparisons every time?
* Because direct insertion is the right place to find the temp.
* Why Temp<arr[j-val] This condition can be put in for?
* Because the original group of data is in order, find the location of the stop is.
* Do not understand to see the direct insertion sort bar.
*/
For (j=i j>=val&&temp<arr[j-val]; j-=val) {
/*
* Why is Arr[j-val] not arr[j]?
* Because J=i started, and the condition is j>=val&&temp<arr[j-val]
*/
ARR[J] = Arr[j-val];
}
/*
* Note not arr[i] = Temp
* The direct insertion sort is also the case.
* For what?
* Because J is the position, I is the element to be inserted
*/
ARR[J] = temp;
}
}
}
}

Three. The question
Is hill sort of right? In other words, how do you choose an incremental sequence to ensure correct (including length, value)? Yes, the last time as long as the guarantee increment is 1 OK (regardless of the sequence length, but the efficiency is low), if the sequence is only 1, that is the direct insertion sort, do not know the right.

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.