-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path堆排序.cpp
112 lines (103 loc) · 2.29 KB
/
堆排序.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<iostream>
using namespace std;
const int maxn = 1005;
class Priority_queue {
public:
Priority_queue():len(0) {}
///Ìí¼ÓÔªËØ
int Push(int num) {
if(len >= maxn-1)
return -1;
m_queue[++len] = num;
int j = len;
while(j != 1) {
int p = j/2;
if(m_queue[p] > num) {
m_queue[j] = m_queue[p];
m_queue[p] = num;
}
j/=2;
}
}
///¶ÓÊ×ÔªËØ
int Front() {
if(len == 0)
return -1;
return m_queue[1];
}
///ɾ³ý¶ÓÊ×ÔªËØ
int Pop() {
if(len > 0) {
m_queue[1] = m_queue[len];
--len;
int i=1;
while(i*2 <= len) {
int j = i*2;
int index, minx;
if(i*2+1 <= len) {
if(m_queue[j] < m_queue[j+1]) {
minx = m_queue[j];
index = j;
} else {
minx = m_queue[j+1];
index = j+1;
}
} else {
index=j;
}
if(m_queue[i] > minx) {
int temp = m_queue[i];
m_queue[i] = m_queue[index];
m_queue[index] = temp;
}
i = index;
}
return 0;
}
return -1;
}
///·µ»Ø¶ÓµÄ³¤¶È
int size() {
return len;
}
private:
int m_queue[maxn];
int len;
};
int main() {
int n, t;
cin >> n;
Priority_queue que;
for(int i=0; i<n; i++) {
cin >> t;
que.Push(t);
}
for(int i=0; i<que.size(); i++) {
while(que.size() != 0) {
cout << que.Front() << " ";
que.Pop();
}
}
}
//int main() {
// int n;
// cin >> n;
// int arr[maxn];
// int t;
// for(int i=0; i<n; i++) {
// cin >> t;
// arr[i] = t;
// int j = i;
// while(j) {
// int p = j/2;
// if(arr[p] > t) {
// arr[j] = arr[p];
// arr[p] = t;
// }
// j/=2;
// }
// }
// for(int i=0; i<n; i++)
// cout << arr[i] << " ";
// return 0;
//}