|
| 1 | +/* |
| 2 | +Sorting: Implement Heap Sort in Java |
| 3 | +*/ |
| 4 | + |
| 5 | +/* |
| 6 | +APPROACH: Build Max Heap: The first step is to build a max heap from the given array. This is done by starting from the middle element of the array and heapifying each subtree in a bottom-up manner. Heapify operation ensures that the largest element is at the root of the subtree. |
| 7 | +
|
| 8 | +Extract Elements: After building the max heap, the largest element (root) is at the top of the heap. We swap it with the last element of the heap and reduce the heap size by 1. Then, we heapify the root to maintain the heap property. We repeat this process until all the elements are extracted and the heap is empty. |
| 9 | +*/ |
| 10 | + |
| 11 | +public class HeapSort { |
| 12 | + |
| 13 | + public void heapSort(int[] array) { |
| 14 | + int length = array.length; |
| 15 | + |
| 16 | + // Build max heap |
| 17 | + for (int i = length / 2 - 1; i >= 0; i--) |
| 18 | + heapify(array, length, i); |
| 19 | + |
| 20 | + // Extract elements from the heap in sorted order |
| 21 | + for (int i = length - 1; i > 0; i--) { |
| 22 | + // Move current root to end |
| 23 | + int temp = array[0]; |
| 24 | + array[0] = array[i]; |
| 25 | + array[i] = temp; |
| 26 | + |
| 27 | + // Heapify the reduced heap |
| 28 | + heapify(array, i, 0); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // Heapify a subtree rooted at index i |
| 33 | + void heapify(int[] array, int length, int i) { |
| 34 | + int largest = i; // Initialize largest as root |
| 35 | + int leftChild = 2 * i + 1; // Left child index |
| 36 | + int rightChild = 2 * i + 2; // Right child index |
| 37 | + |
| 38 | + // If left child is larger than root |
| 39 | + if (leftChild < length && array[leftChild] > array[largest]) |
| 40 | + largest = leftChild; |
| 41 | + |
| 42 | + // If right child is larger than largest so far |
| 43 | + if (rightChild < length && array[rightChild] > array[largest]) |
| 44 | + largest = rightChild; |
| 45 | + |
| 46 | + // If largest is not the root |
| 47 | + if (largest != i) { |
| 48 | + // Swap the root with the largest element |
| 49 | + int swap = array[i]; |
| 50 | + array[i] = array[largest]; |
| 51 | + array[largest] = swap; |
| 52 | + |
| 53 | + // Recursively heapify the affected subtree |
| 54 | + heapify(array, length, largest); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Utility function to print an array |
| 59 | + void printArray(int[] array) { |
| 60 | + int length = array.length; |
| 61 | + for (int i = 0; i < length; ++i) |
| 62 | + System.out.print(array[i] + " "); |
| 63 | + System.out.println(); |
| 64 | + } |
| 65 | + |
| 66 | + public static void main(String[] args) { |
| 67 | + int[] array = { 12, 11, 13, 5, 6, 7 }; |
| 68 | + int length = array.length; |
| 69 | + |
| 70 | + HeapSort heapSort = new HeapSort(); |
| 71 | + heapSort.heapSort(array); |
| 72 | + |
| 73 | + System.out.println("Sorted array: "); |
| 74 | + heapSort.printArray(array); |
| 75 | + } |
| 76 | +} |
| 77 | +/* |
| 78 | +Time Complexity: The time complexity of Heap Sort is O(n log n), where n is the number of elements in the array. The initial heap construction takes O(n) time, and the repeated heapify operation during extraction takes O(log n) time. As we perform heapify for each element, the overall time complexity is O(n log n). |
| 79 | +
|
| 80 | +Space Complexity: The space complexity of Heap Sort is O(1) since the sorting is performed in-place, without requiring any additional space proportional to the input size. |
| 81 | +*/ |
| 82 | + |
0 commit comments