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)