|
| 1 | +// A C++ program to find k'th smallest element using max heap |
| 2 | +#include<iostream> |
| 3 | +#include<climits> |
| 4 | +using namespace std; |
| 5 | + |
| 6 | +// Prototype of a utility function to swap two integers |
| 7 | +void swap(int *x, int *y); |
| 8 | + |
| 9 | +// A class for Max Heap |
| 10 | +class MaxHeap |
| 11 | +{ |
| 12 | + int *harr; // pointer to array of elements in heap |
| 13 | + int capacity; // maximum possible size of max heap |
| 14 | + int heap_size; // Current number of elements in max heap |
| 15 | +public: |
| 16 | + MaxHeap(int a[], int size); // Constructor |
| 17 | + void maxHeapify(int i); //To maxHeapify subtree rooted with index i |
| 18 | + int parent(int i) { return (i-1)/2; } |
| 19 | + int left(int i) { return (2*i + 1); } |
| 20 | + int right(int i) { return (2*i + 2); } |
| 21 | + |
| 22 | + int extractMax(); // extracts root (maximum) element |
| 23 | + int getMax() { return harr[0]; } // Returns maximum |
| 24 | + |
| 25 | + // to replace root with new node x and heapify() new root |
| 26 | + void replaceMax(int x) { harr[0] = x; maxHeapify(0); } |
| 27 | +}; |
| 28 | + |
| 29 | +MaxHeap::MaxHeap(int a[], int size) |
| 30 | +{ |
| 31 | + heap_size = size; |
| 32 | + harr = a; // store address of array |
| 33 | + int i = (heap_size - 1)/2; |
| 34 | + while (i >= 0) |
| 35 | + { |
| 36 | + maxHeapify(i); |
| 37 | + i--; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// Method to remove maximum element (or root) from max heap |
| 42 | +int MaxHeap::extractMax() |
| 43 | +{ |
| 44 | + if (heap_size == 0) |
| 45 | + return INT_MAX; |
| 46 | + |
| 47 | + // Store the maximum vakue. |
| 48 | + int root = harr[0]; |
| 49 | + |
| 50 | + // If there are more than 1 items, move the last item to root |
| 51 | + // and call heapify. |
| 52 | + if (heap_size > 1) |
| 53 | + { |
| 54 | + harr[0] = harr[heap_size-1]; |
| 55 | + maxHeapify(0); |
| 56 | + } |
| 57 | + heap_size--; |
| 58 | + |
| 59 | + return root; |
| 60 | +} |
| 61 | + |
| 62 | +// A recursive method to heapify a subtree with root at given index |
| 63 | +// This method assumes that the subtrees are already heapified |
| 64 | +void MaxHeap::maxHeapify(int i) |
| 65 | +{ |
| 66 | + int l = left(i); |
| 67 | + int r = right(i); |
| 68 | + int largest = i; |
| 69 | + if (l < heap_size && harr[l] > harr[i]) |
| 70 | + largest = l; |
| 71 | + if (r < heap_size && harr[r] > harr[largest]) |
| 72 | + largest = r; |
| 73 | + if (largest != i) |
| 74 | + { |
| 75 | + swap(&harr[i], &harr[largest]); |
| 76 | + maxHeapify(largest); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// A utility function to swap two elements |
| 81 | +void swap(int *x, int *y) |
| 82 | +{ |
| 83 | + int temp = *x; |
| 84 | + *x = *y; |
| 85 | + *y = temp; |
| 86 | +} |
| 87 | + |
| 88 | +// Function to return k'th largest element in a given array |
| 89 | +int kthSmallest(int arr[], int n, int k) |
| 90 | +{ |
| 91 | + // Build a heap of first k elements: O(k) time |
| 92 | + MaxHeap mh(arr, k); |
| 93 | + |
| 94 | + // Process remaining n-k elements. If current element is |
| 95 | + // smaller than root, replace root with current element |
| 96 | + for (int i=k; i<n; i++) |
| 97 | + if (arr[i] < mh.getMax()) |
| 98 | + mh.replaceMax(arr[i]); |
| 99 | + |
| 100 | + // Return root |
| 101 | + return mh.getMax(); |
| 102 | +} |
| 103 | + |
| 104 | +// Driver program to test above methods |
| 105 | +int main() |
| 106 | +{ |
| 107 | + int arr[] = {12, 3, 5, 7, 19}; |
| 108 | + int n = sizeof(arr)/sizeof(arr[0]), k = 4; |
| 109 | + cout << "K'th smallest element is " << kthSmallest(arr, n, k); |
| 110 | + return 0; |
| 111 | +} |
0 commit comments