|  | 
|  | 1 | +#include<iostream> | 
|  | 2 | +using namespace std; | 
|  | 3 | + | 
|  | 4 | + | 
|  | 5 | +void maxHeapify(int*, int, int); | 
|  | 6 | +void buildHeap(int*, int); | 
|  | 7 | +void heapsort(int*, int); | 
|  | 8 | + | 
|  | 9 | +int main() { | 
|  | 10 | + | 
|  | 11 | +	int arr[] = { 4,2,3,1,5,6,8,}; | 
|  | 12 | + | 
|  | 13 | +	heapsort(arr, sizeof(arr) / sizeof(int)); | 
|  | 14 | + | 
|  | 15 | +	for (int i = 0; i < sizeof(arr) / sizeof(int); i++) { | 
|  | 16 | +		cout << arr[i] << " "; | 
|  | 17 | +	} | 
|  | 18 | + | 
|  | 19 | +	return 0; | 
|  | 20 | +} | 
|  | 21 | + | 
|  | 22 | +void maxHeapify(int* arr, int n, int i) { | 
|  | 23 | +	if (i > n / 2 - 1) { | 
|  | 24 | +		//if index of element is greater than i/2 it is in the last level of the heap. no need to call maxHeapify for it. | 
|  | 25 | +		return; | 
|  | 26 | +	} | 
|  | 27 | +	if (arr[i] > arr[2 * i] && arr[i] > arr[(2 * i) + 1]) { | 
|  | 28 | +		//heap property already satisfied. | 
|  | 29 | +		return; | 
|  | 30 | +	} | 
|  | 31 | +	 | 
|  | 32 | +	int maxInx = i, temp; | 
|  | 33 | + | 
|  | 34 | +	while (i <= (n / 2 - 1)) { | 
|  | 35 | +		//finding the index of the largest child of i. | 
|  | 36 | +		if (arr[2 * i + 1] > arr[i] && (2 * i + 1) < n) { | 
|  | 37 | +			maxInx = 2 * i + 1; | 
|  | 38 | +		} | 
|  | 39 | +		if (arr[(2 * i) + 2] > arr[maxInx] && (2 * i + 2) < n) { | 
|  | 40 | +			maxInx = (2 * i) + 2; | 
|  | 41 | +		} | 
|  | 42 | +		//if no change occurs. then return. | 
|  | 43 | +		if (i == maxInx) { return; } | 
|  | 44 | + | 
|  | 45 | +		//having obtained the index of the largest node swap it with i. | 
|  | 46 | +		//swapping arr[i] with arr[maxInx] | 
|  | 47 | +		temp = arr[maxInx]; | 
|  | 48 | +		arr[maxInx] = arr[i]; | 
|  | 49 | +		arr[i] = temp; | 
|  | 50 | +		i = maxInx; | 
|  | 51 | +	} | 
|  | 52 | + | 
|  | 53 | +} | 
|  | 54 | + | 
|  | 55 | +void buildHeap(int* arr, int n) { | 
|  | 56 | +	//start calling from the last parent in the heap(n/2) and go upto the root node. | 
|  | 57 | +	//start from the last parent instead of root because here the loop invacriant is that heap condition is  | 
|  | 58 | +	//satisfied for all nodes other than the one for which it is called. which would not be true when it is  | 
|  | 59 | +	//called for the root node of the array when the array is compeletely random. | 
|  | 60 | +	//refer to CORMEN for more details. | 
|  | 61 | +	for (int i = (n / 2 - 1); i >= 0; i--) { | 
|  | 62 | +		maxHeapify(arr, n, i); | 
|  | 63 | +	} | 
|  | 64 | +} | 
|  | 65 | + | 
|  | 66 | +void heapsort(int* arr, int n) { | 
|  | 67 | +	buildHeap(arr, n); | 
|  | 68 | +	 | 
|  | 69 | +	int temp; | 
|  | 70 | +	while (n > 0) { | 
|  | 71 | +	//after the heap is built take the element a[0], swap it with the last element, discard it and call maxheapify  | 
|  | 72 | +	//for the root element | 
|  | 73 | + | 
|  | 74 | +		temp = arr[n - 1]; | 
|  | 75 | +		arr[n - 1] = arr[0]; | 
|  | 76 | +		arr[0] = temp; | 
|  | 77 | +		n--; | 
|  | 78 | +		maxHeapify(arr, n, 0); | 
|  | 79 | +	} | 
|  | 80 | +} | 
0 commit comments