2019-05-09 19:32:54 +08:00
|
|
|
package Misc;
|
|
|
|
|
|
|
|
public class heap_sort {
|
2019-05-10 10:25:19 +08:00
|
|
|
public void sort(int[] arr) {
|
2017-10-05 12:51:24 +08:00
|
|
|
int n = arr.length;
|
2019-05-09 19:32:54 +08:00
|
|
|
|
2017-10-05 12:51:24 +08:00
|
|
|
// Build heap (rearrange array)
|
|
|
|
for (int i = n / 2 - 1; i >= 0; i--)
|
|
|
|
heapify(arr, n, i);
|
2019-05-09 19:32:54 +08:00
|
|
|
|
2017-10-05 12:51:24 +08:00
|
|
|
// One by one extract an element from heap
|
2019-05-09 19:32:54 +08:00
|
|
|
for (int i = n - 1; i >= 0; i--) {
|
2017-10-05 12:51:24 +08:00
|
|
|
// Move current root to end
|
|
|
|
int temp = arr[0];
|
|
|
|
arr[0] = arr[i];
|
|
|
|
arr[i] = temp;
|
2019-05-09 19:32:54 +08:00
|
|
|
|
2017-10-05 12:51:24 +08:00
|
|
|
// call max heapify on the reduced heap
|
|
|
|
heapify(arr, i, 0);
|
|
|
|
}
|
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
|
2017-10-05 12:51:24 +08:00
|
|
|
// To heapify a subtree rooted with node i which is
|
|
|
|
// an index in arr[]. n is size of heap
|
2019-05-10 10:25:19 +08:00
|
|
|
void heapify(int[] arr, int n, int i) {
|
2017-10-05 12:51:24 +08:00
|
|
|
int largest = i; // Initialize largest as root
|
2019-05-09 19:32:54 +08:00
|
|
|
int l = 2 * i + 1; // left = 2*i + 1
|
|
|
|
int r = 2 * i + 2; // right = 2*i + 2
|
|
|
|
|
2017-10-05 12:51:24 +08:00
|
|
|
// If left child is larger than root
|
|
|
|
if (l < n && arr[l] > arr[largest])
|
|
|
|
largest = l;
|
2019-05-09 19:32:54 +08:00
|
|
|
|
2017-10-05 12:51:24 +08:00
|
|
|
// If right child is larger than largest so far
|
|
|
|
if (r < n && arr[r] > arr[largest])
|
|
|
|
largest = r;
|
2019-05-09 19:32:54 +08:00
|
|
|
|
2017-10-05 12:51:24 +08:00
|
|
|
// If largest is not root
|
2019-05-09 19:32:54 +08:00
|
|
|
if (largest != i) {
|
2017-10-05 12:51:24 +08:00
|
|
|
int swap = arr[i];
|
|
|
|
arr[i] = arr[largest];
|
|
|
|
arr[largest] = swap;
|
2019-05-09 19:32:54 +08:00
|
|
|
|
2017-10-05 12:51:24 +08:00
|
|
|
// Recursively heapify the affected sub-tree
|
|
|
|
heapify(arr, n, largest);
|
|
|
|
}
|
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
|
2017-10-05 12:51:24 +08:00
|
|
|
/* A utility function to print array of size n */
|
2019-05-10 10:25:19 +08:00
|
|
|
static void printArray(int[] arr) {
|
2017-10-05 12:51:24 +08:00
|
|
|
int n = arr.length;
|
2019-05-09 19:32:54 +08:00
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
System.out.print(arr[i] + " ");
|
2017-10-05 12:51:24 +08:00
|
|
|
System.out.println();
|
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
|
2017-10-05 12:51:24 +08:00
|
|
|
// Driver program
|
2019-05-09 19:32:54 +08:00
|
|
|
public static void main(String args[]) {
|
2017-10-05 12:51:24 +08:00
|
|
|
int arr[] = {12, 11, 13, 5, 6, 7};
|
|
|
|
int n = arr.length;
|
2019-05-09 19:32:54 +08:00
|
|
|
|
2017-12-17 00:20:22 +08:00
|
|
|
heap_sort ob = new heap_sort();
|
2017-10-05 12:51:24 +08:00
|
|
|
ob.sort(arr);
|
2019-05-09 19:32:54 +08:00
|
|
|
|
2017-10-05 12:51:24 +08:00
|
|
|
System.out.println("Sorted array is");
|
|
|
|
printArray(arr);
|
|
|
|
}
|
|
|
|
}
|