Package sort;
Import Java.util.Random;
public class Heapsort {public static int SIZE = 10;
public static class Heap {public int[] array;
public int size;
Public Heap (int[] array) {super ();
This.array = array;
This.size = array.length-1;
public int Length () {return array.length;
public void Exchange (int i, int j) {Array[i] = Array[i] ^ array[j];
ARRAY[J] = Array[i] ^ array[j];
Array[i] = Array[i] ^ array[j];
The method is executed on the assumption that two subtrees are the largest heap public static void Maxheapify (Heap A, int i) {int left = i << 1;
int right = left + 1;
int max = i;
if (left <= a.size && A.array[left] > A.array[i]) {max = left;
} if (right <= a.size && a.array[right] > A.array[max]) {max = right;
} if (max!= i) { A.exchange (max, i);
Maxheapify (A, max);
} public static void Buildmaxheap (Heap a) {for (int i = a.size >> 1; i > 0; i--) {
Maxheapify (A, I);
} public static void Heapsort (Heap a) {buildmaxheap (a);
int s = a.size;
for (int i = s; I >= 2; i--) {A.exchange (I, 1);
a.size--;
Maxheapify (A, 1);
} public static int Heapmaxvalue (Heap a) {return a.array[1]; public static int poll (Heap a) throws Exception {if (a.size = 0) {throw new Exception ("Heap
is empty! ");}
int max = a.array[1];
A.ARRAY[1] = a.array[a.size--];
Maxheapify (A, 1);
return Max;
public static void Heapincreasekey (Heap A, int i, int key) throws Exception {if (A.array[i] > key) {
throw new Exception ();
} A.array[i] = key;while (i > 1 && a.array[(i >> 1)] < key) {A.exchange (i, I >> 1);
i = i >> 1;
} public static void Main (string[] args) {int[] a = new int[size];
Random Random = new Random (100);
for (int i = 0; i < SIZE; i++) {A[i] = (size-i) * (size-i);
} Heap array = new Heap (a);
Heapsort (array);
for (int i = 1; i < Array.Length (); i++) {System.out.println (array.array[i));
}
}
}