Java Hill Sort

Source: Internet
Author: User

Java Hill Sort 1, basic idea:

The hill sort also becomes "reduced incremental sort", the basic principle is that the array elements to be sorted into several sub-sequences, so that the number of elements of each sub-sequence is relatively small, and then the individual sub-sequences are directly inserted into the order of the entire sequence to be ordered "basic order" after the end of all elements in a direct insertion sort. Therefore, we should adopt the strategy of jumping segmentation: to make a sub-sequence of records that are separated by an "increment", so as to ensure that the results obtained by direct insertion in the subsequence are basically ordered rather than partially ordered. Hill Sort is the optimization and upgrade of the direct insertion sorting algorithm.
The so-called basic order, is the small keyword basic in front, large basic in the back, the basic in the middle, for example {2,1,3,6,4,7,5,8,9,} can be called basic order. But like {1,5,9,3,7,8,2,4,6} so, 9 in the third place, 2 in the last third place is not basically orderly.

2. Analysis of Complexity:

The key to the hill sort is not to sort by random groupings, but to make a sub-sequence of records separated by an "increment", which makes the order more efficient. It is important to note that the last increment of the increment sequence must be equal to 1. In addition, Hill sorting is not a stable sorting algorithm because the record is a jump-through movement .
The best time-complexity and average-time complexity of hill sorting is the worst time complexity.

3. The sorting process is as follows:

In the case of arrays {26, 53, 67, 48, 57, 13, 48, 32, 60, 50}, the step sequence is {5,2,1}
Initialization keywords: [26, 53, 67, 48, 57, 13, 48, 32, 60, 50]

Final sort Result:
13 26 32 48 48 50 53 57 60 67

4, Algorithm analysis:

Start with array[0], insert the sort directly in increments of Incrementnum, until the end of the array, and then repeat from array[1]: Direct insert sorting with incrementnum increments; Then start repeating from array[1] ... Until Array[n].

Then take a new increment that is less than the previous increment (for example, set to INCREMENTNUM/2), iterate over the result array of the previous step, and insert the sort directly ....

Then take the new increment less than the previous increment, repeat: traverse, insert sort directly

Exit the loop until the new increment is less than 1

For example, there are arrays {82, 31, 29, 71, 72, 42, 64, 5,110} The first fetch increment is set to ARRAY.LENGTH/2 = 4 starts with 82 increments from 4 to the end, gets (82,42) sorted by {42, 31, 29, 7 1, 72, 82, 64, 5,110}.   Then repeat the previous step from the second number 31 to get (31,64) sorted by {42, 31, 29, 71, 72, 82, 64, 5,110} .... After iterating through the array in increments of 4, the resulting result is {42,31,5,71,72,82,64,29,110}

Then the area increment is set to INCREMENTNUM/2 = 2, repeat step 1 for {42, 31,5,71,72,82,64,29,110}. After you're done, repeat step 1 for the new increment. Exits the loop until the increment is less than 1.

For example, there is a situation where an unordered array is ordered from small to large, but the number of the last position of the array is the smallest, we want to move it to the first position, the other position will be moved backwards, if the array is very large, then the direct insertion of the cost is very large.

5. code example:
 Packagesort;Importjava.util.Arrays;/*** Hill Sort * *@authorTangjiang November 24, 2017 afternoon 4:51:37 **/ Public classShellsort { Public Static voidShellsorttest (int[] arr) {        if(arr = =NULL|| Arr.length <= 1) {            return; }        //Set initial increment        intIncrease = ARR.LENGTH/2;  while(Increase >= 1) {             for(inti = 0; i < arr.length; i++) {                //make a jump insert sort                 for(intj = i; J < Arr.length-increase; j = j +Increase) {                    //Compare size Exchange data                    if(Arr[j] > arr[j +Increase]) {                        inttemp =Arr[j]; ARR[J]= Arr[j +increase]; Arr[j+ Increase] =temp; }                }            }            //set a new incrementIncrease = INCREASE/2; }    }     Public Static voidMain (string[] args) {int[] A = {-1, 3, 6, 2, 6, 89, 3 }; Shellsorttest (a);//for (int i:a) {//System.out.println (i + "");//        }System.out.println (Arrays.tostring (a)); }}

Java 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.