Sort--Hill sort __ Data structure

Source: Internet
Author: User
The order of the ER (Shell sort) is a sort of insertion. Also called narrowing incremental ordering, is a more efficient version of the direct insertion sort algorithm. Hill sort is a non stable sort algorithm. The method is due to a DL. The shell was named after it was introduced in 1959. Hill sort is to the record by a certain increment of the subscript group, the use of a direct insertion sort algorithm for each group; As the increment gradually decreases, each group contains more and more keywords, when the increment is reduced to 1 o'clock, the whole file is divided into a group, the algorithm will terminate. Hill sort is an unstable sort. Basic IdeasFirst, take a integer d1 less than n as the first increment, grouping all the records of the file. 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 =1 (<. <d2<d1) is taken, where all records are placed in the same group for direct insertion sort.
Example: There is a sequence: 4, 9, 7, 1, 3, 8, 2, 0
The increment is half of the sequence length, or 4, and the sequence is divided into 4 subsequence a,b,c,d;

The four sequences are sorted separately, followed by the following columns: 3, 8, 2, 0, 4, 9, 7, 1, 4, 2, and the sequence into two sub sequences a,b
Sort two subsequence sequence 2, 0, 3, 1, 4, 8, 7, 9
And then take the increment of 2, or 1, to sort the entire sequence to the final sort result
0, 1, 2, 3, 4, 7, 8, 9 Java code implementation:
public static void Shellsort (int[] Data {
		 //len array length
		 int len = data.length;
		 G incremental  Initial increment half of the array length for
		 (int g = len/2;g>0;g/=2) {
			 for (int i=g;i<len;i++) {
				 //temporary Save current value
				 int TMP =data[i];
				 int j = i-g;
				 If the current value is small and all previous values in the sequence are compared, the most total determines the correct position of the current value in the sequence while
				 (J>=0&&data[j] > tmp) {
						 data[j+g]=data[j] ;
						 j-=g;
				 }
				 Data[j+g]=tmp
			 }}
		 }
	 


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.