forked from skully-coder/competitiveprogramming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap_sort.cpp
60 lines (56 loc) · 737 Bytes
/
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
#include <iostream>
using namespace std;
void Insert(int A[], int n)
{
int i = n, temp;
temp = A[i];
while (i > 1 && temp > A[i / 2])
{
A[i] = A[i / 2];
i = i / 2;
}
A[i] = temp;
}
int Delete(int A[], int n)
{
int i, j, x, temp, val;
val = A[1];
x = A[n];
A[1] = A[n];
A[n] = val;
i = 1; j = i * 2;
while (j < n - 1)
{
if (A[j + 1] > A[j])
j = j + 1;
if (A[i] < A[j])
{
temp = A[i];
A[i] = A[j];
A[j] = temp;
i = j;
j = 2 * j;
}
else
break;
}
return val;
}
int main()
{
int H[] = { 0,10,20,30,25,5,40,35 };
int i;
for (i = 2; i <= 7; i++)
{
Insert(H, i);
}
for (i = 7; i > 1; i--)
{
Delete(H, i);
}
for (i = 1; i <= 7; i++)
{
cout << H[i] <<" ";
}
cout << endl;
}