Hill sort Java Implementation

Source: Internet
Author: User

 PackageKpp.sort;/*** Hill Sort * 1. Set the step d, dividing every D element into a group, inserting a sort within the group; * 2.d/=2, repeat 1 steps until the step is 1 * We know once the insertion sort is stable, but during different insertion sorts, the same elements may move in the respective insert sort,  Finally, the stability will be disrupted, so the hill sort is unstable.  The time performance of the Hill sort is better than the direct insertion sort, for the following reasons: (1) Fewer comparisons and moves are required when the initial state of the file is basically ordered.  (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). * @authorKPP **/ Public classShellsort { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        intArray[] = {49,38,65,97,176,213,227,49,78,34,12,164,11,18,1};        Shellsort (array);  for(intK:array)        {System.out.println (k); }    }    Private Static intShellsort (intdata[]) {        intLen =data.length; inttemp = 0;  for(intincrement = LEN/2; Increment > 0; Increment/= 2) {            intj = 0;  for(inti = increment; i < Len; i++) {Temp=Data[i];  for(j = i-increment; J >= 0; J-=increment) {                      if(Temp <Data[j]) {Data[j+increment] =Data[j]; }Else{                           Break; }} data[j+increment] =temp; }          }          return0; }}

Hill sort Java Implementation

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.