-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap_sort.cpp
More file actions
76 lines (62 loc) · 1.56 KB
/
heap_sort.cpp
File metadata and controls
76 lines (62 loc) · 1.56 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
#include <iostream>
/* Heap sort steps
--------------------------------------------------------------------------------
1. Build heap from unordered array.
2. Find max element from array.
3. Swap elements A[heap_size] with A[0], now max element is at the end of array.
4. Discard node n from heap, decrementing heap size.
5. New root may violate max heap but children are max heaps, call max_heapify().
*/
void print(int[], int);
void heap_sort(int[], int);
void build_heap(int[], int);
void max_heapify(int[], int, int);
int main()
{
int array[] = {-10, -3, 20, 18, 9};
int array_size = sizeof(array) / sizeof(*array);
heap_sort(array, array_size);
print(array, array_size);
return 0;
}
//Prints the array
void print(int A[], int n)
{
for (int i = 0; i < n; i++)
std::cout << A[i] << " ";
std::cout << std::endl;
}
//Performs heap sort
void heap_sort(int A[], int n)
{
build_heap(A, n);
//Heap sort
for (int i = n; i > 0; i--)
{
std::swap(A[0], A[i - 1]);
max_heapify(A, i - 1, 1);
}
}
//Builds heap structures from given array
void build_heap(int A[], int n)
{
for (int i = n / 2; i > 0; i--)
max_heapify(A, n, i);
}
void max_heapify(int A[], int n, int i)
{
// Find largest among root, left child and right child
int largest = i;
int left = 2 * i;
int right = 2 * i + 1;
if (left <= n && A[left - 1] > A[largest - 1])
largest = left;
if (right <= n && A[right - 1] > A[largest - 1])
largest = right;
// Swap and continue heapifying if root is not largest
if (largest != i)
{
std::swap(A[i - 1], A[largest - 1]);
max_heapify(A, n, largest);
}
}