-
Notifications
You must be signed in to change notification settings - Fork 47
/
heap sort
60 lines (42 loc) Β· 1.09 KB
/
heap sort
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 heap1(int list[], int number, int i)
{
int largest = i;
int left = 2*i + 1;
int right = 2*i + 2;
if (left < number && list[left] > list[largest])
largest = left;
if (right < number && list[right] > list[largest])
largest = right;
// If largest is not root
if (largest != i)
{
swap(list[i], list[largest]);
heap1(list, number, largest);
}
}
void heapSort(int list[], int number)
{
for (int i = number / 2 - 1; i >= 0; i--)
heap1(list, number, i);
for (int i=number-1; i>0; i--)
{
swap(list[0], list[i]);
heap(list, i, 0);
}
}
void printArray(int list[], int number)
{
for (int i=0; i<number; ++i)
cout << list[i] << " ";
cout << "\n";
}
int main()
{
int list[] = {32, 41, 53, 91, 36, 17};
int number = sizeof(list)/sizeof(list[0]);
heapSort(list, number);
cout << "Sorted array is \n";
printArray(list, number);
}