1, the code is as follows
PackageBetter.amy.sort;/*** Heap Sort Implementation * *@authorZhujinrong **/ Public classHeapsort {/*** The result of constructing a large heap of large piles is the ascending order * *@paramA * array A *@paramm * Start position *@paramn * End Position*/ Private Static voidHeapadjustmax (intA[],intMintN) {intt =A[m]; for(inti = 2 * m + 1; I <= N; i = 2 * i + 1) { if(I < n && a[i + 1] >A[i]) {i++; } if(T <A[i]) {A[m]=A[i]; M=i; }} A[m]=T; } /*** Ascending Heap Sort * *@paramA * array to be sorted*/ Public Static voidHEAPSORTASC (inta[]) { if(A = =NULL) { return; } intn =a.length; for(inti = N/2-1; I >= 0; i--) {Heapadjustmax (A, I, n-1); } for(inti = n-1; i > 0; i--) { intt =A[i]; A[i]= A[0]; a[0] =T; Heapadjustmax (A,0, I-1); } } /*** from subscript m, to N, adjust it to small heap small Gan sort result is descending (or non-ascending) * *@paramA * array to be sorted *@paramm * Start position *@paramn * End Position*/ Public Static voidHeapadjustmin (int[] A,intMintN) {intt =A[m]; for(inti = 2 * m + 1; I <= N; i = 2 * i + 1) { if(I < n && a[i + 1] <A[i]) {i++; } if(T >A[i]) {A[m]=A[i]; M=i; }} A[m]=T; } /*** Descending heap sort * *@paramA * array to be sorted*/ Public Static voidHeapsortdesc (inta[]) { if(A = =NULL) { return; } intn =a.length; for(inti = N/2-1; I >= 0; i--) {Heapadjustmin (A, I, n-1); } for(inti = n-1; i > 0; i--) { intt =A[i]; A[i]= A[0]; a[0] =T; Heapadjustmin (A,0, I-1); } }}
Heap Sort Implementation