Hill sort and Heap Sort (Java edition)

Source: Internet
Author: User

Hill Sort


Package Shellsort;import utils.sortutils; Public classDemo { Public Static void Shellsort(int[] a) {intTemp,j;intincrement = A.length; Do{increment = increment/3+1;//If it is a direct insert sort, the increment here are 1.             //system.out.println (increment);             for(inti = increment; i < a.length; i++) {if(A[i]<a[i-increment]) {temp = A[i];//Staged in temp                     for(J=i-increment; j>=0&& a[j]>temp; j-=increment) {A[j+increment] = a[j];//record and move back to find the insertion position} A[j+increment] = temp;//Insert}            }        } while(increment>1); } Public Static void Main(string[] args) {int[] A = {2,3,5,4,1,6,9,8,7};        Shellsort (a);    Sortutils.printstring (a); }}
Analysis of Complexity


Heap Sort

 PackageHeapsort;ImportUtils.sortutils; Public  class Demo02 {    //Heap sort     Public Static void Heapsort(int[] a) {intlen = a.length;//Build the data to be sorted into a large top heap         for(intI= (a.length-1-1)/2; i>=0; i--) {heapadjust (A,i,len); } for(inti = a.length-1; i >0; i--) {//Exchange The top value of the team with the last value of the current unsorted subsequenceSortutils.swap (A,0, i); Heapadjust (A,0,--Len);//re-adjust the large top pile}    }//Build the sequence you want to sort into a large top heap     Public Static void Heapadjust(int[] A,intSintLen) {intTemp temp = A[s]; for(inti =2*s+1; i < Len; i++) {if(i<len-1&& a[i]<a[i+1]) {i++;//i is the subscript for the larger record in the keyword}if(Temp>=a[i]) {//If the parent node itself is greater than the maximum child node, terminate the loop                 Break;            } A[s] = A[i];        s = i;    } A[s] = temp; } Public Static void Main(string[] args) {int[] A = {2,3,5,4,1,6,9,8,7};        Heapsort (a);    Sortutils.printstring (a); }}
Analysis of Complexity


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hill sort and Heap Sort (Java edition)

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.