-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.cpp
More file actions
39 lines (34 loc) · 868 Bytes
/
HeapSort.cpp
File metadata and controls
39 lines (34 loc) · 868 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
35
36
37
38
39
// stl 없이 힙 정렬 구현
// 시간 복잡도 best - avg - worst >>> nlogn - nlogn - nlogn
#include <iostream>
using namespace std;
int N;
int arr[1000001];
void heap(int num){
for(int i=1; i<num; i++){
int child = i;
while (child > 0){
int root = (child-1)/2;
if(arr[root] < arr[child]){
int temp = arr[root];
arr[root] = arr[child];
arr[child] = temp;
}
child = root;
}
}
}
int main(void){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> N;
for(int i=0; i<N; i++){ cin >> arr[i]; }
heap(N);
for(int i=N-1; i>=0; i--){
int temp = arr[i];
arr[i] = arr[0];
arr[0] = temp;
heap(i);
}
for(int i=0; i<N; i++){ cout << arr[i] <<"\n"; }
return 0;
}