-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheadSortArr.cpp
More file actions
80 lines (66 loc) · 1.27 KB
/
headSortArr.cpp
File metadata and controls
80 lines (66 loc) · 1.27 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
using namespace std;
inline void swap( int &n1, int &n2){
int temp = n1;
n1 = n2;
n2 = temp;
}
//a[0] to a[n-1] is a max piority heap
void upElement(int *a, int n){
if(n<=0) //root
return ;
int parent = (n-1)/2;
if(a[n] > a[parent]){
swap( a[n], a[parent]);
upElement(a,parent);
}
}
//a[0] just added, a[1] to a[n] need a root
void downElement(int *a, int n){
if(n<=0)//leaf
return;
int firstChild =1;
int maxIndex = 0;
if(a[maxIndex] < a[firstChild])
maxIndex = firstChild;
if(firstChild +1 <= n && a[maxIndex] < a[firstChild + 1])
maxIndex = firstChild + 1;
if(!maxIndex)//keep
return;
else{
swap(a[0],a[maxIndex]);
downElement(a+maxIndex, n - maxIndex);
}
}//down
//init
void initMaxHeap(int *a, int n){
if(n <= 1)
return ;
int i;
for(i = 1; i < n; ++i)
upElement(a,i);
}
//heap sort
void heapSort(int *a, int n){
initMaxHeap(a,n);
int i;
while(--n){
swap(a[0],a[n]);
downElement(a,n-1);
// initMaxHeap(a,n);
}
}//heap sort end
void print(int *a, int n){
for(int i = 0; i < n; ++i)
cout <<a[i] << " ";
cout<< endl;
}
int main(){
int a[10001];
int n = 0;
while(cin >> a[n]) ++n;
print(a,n);
heapSort(a,n);
print(a,n);
return 0;
}//main