-
Notifications
You must be signed in to change notification settings - Fork 1
/
In-place heap sort.cpp
67 lines (54 loc) · 1.54 KB
/
In-place heap sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
In-place heap sort
Given an integer array of size N. Sort this array (in decreasing order) using heap sort.
Note: Space complexity should be O(1).
Input Format:
The first line of input contains an integer, that denotes the value of the size of the array or N.
The following line contains N space separated integers, that denote the value of the elements of the array.
Output Format :
The first and only line of output contains array elements after sorting. The elements of the array in the output are separated by single space.
Constraints :
1 <= n <= 10^6
Time Limit: 1 sec
Sample Input 1:
6
2 6 8 5 4 3
Sample Output 1:
8 6 5 4 3 2
*/
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void minHeapify(int *arr, int n, int index) {
int curr = index;
int leftChildIndex = 2 * index + 1;
int rightChildIndex = 2 * index + 2;
if(leftChildIndex < n and arr[leftChildIndex] < arr[curr]) {
curr = leftChildIndex;
}
if(rightChildIndex < n and arr[rightChildIndex] < arr[curr]) {
curr = rightChildIndex;
}
if(curr != index) {
swap(&arr[curr], &arr[index]);
minHeapify(arr, n, curr);
}
}
void heapSort(int arr[], int n) {
// Write your code
int count = n - 1;
while(count >= 0){
minHeapify(arr, n, count);
count--;
}
count = 0;
while(count < n) {
swap(&arr[0], &arr[n - count - 1]);
minHeapify(arr, n - count - 1, 0);
count++;
}
}
// Time Complexity : O(nlogn)
// Auxillary Space : O(1)