-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHeap_Sort.cpp
More file actions
34 lines (26 loc) · 752 Bytes
/
Heap_Sort.cpp
File metadata and controls
34 lines (26 loc) · 752 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int main() {
// Taking the length of the array
int n;
cin >> n;
vector<int> arr(n);
// Taking the array elements and storing it in arr.
for(int i = 0; i < n; i++) {
cin >> arr[i];
}
// Printing the input array without sort
cout << "Your unsorted array: ";
for(int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
make_heap(arr.begin(), arr.end()); // Building the heap from the array.
sort_heap(arr.begin(), arr.end()); // Sorting the heap
// Printing the sorted array
cout << "Your sorted array: ";
for(int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}