According to the implementation in the introduction to algorithms, the subscript is changed from 0.
Principle:
Import java. util. arrays; public class solution {/*** each time the top element of the heap is switched to the end and removed from the heap. ** @ Param A */public static void heapsort (INT [] A) {buildheap (a); int n =. length-1; for (INT I =. length-1; I> 0; I --) {swap (A, 0, I); // The heap size minus 1 maxheapify (A, n --, 0) ;}}/*** O (n) Time heap creation */Private Static void buildheap (INT [] A) {for (INT I =. length/2; I> = 0; I --) maxheapify (A,. length, I);}/*** n indicates the current heap size */Private Static void maxheapify (INT [] A, int N, int I) {int L = L EFT (I); int r = right (I); int largest = I; If (L <n & A [l]> A [I]) largest = L; if (r <n & A [R]> A [Largest]) largest = r; If (largest! = I) {swap (A, I, largest); maxheapify (A, N, largest) ;}} Private Static void swap (INT [] A, int I, Int J) {int TMP = A [I]; A [I] = A [J]; A [J] = TMP;} Private Static int left (int I) {return 2 * I + 1;} Private Static int right (int I) {return 2 * I + 2;} public static void main (string [] ARGs) {int [] A = {2, 8, 7, 1, 3, 5, 6, 4}; system. out. println ("Before sorting:" + arrays. tostring (a); heapsort (a); system. out. println ("after sortint:" + arrays. tostring ());}}