forked from AllAlgorithms/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap_sort.cpp
70 lines (60 loc) · 1.42 KB
/
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
61
62
63
64
65
66
// HeapSorted Implementation (based on CLRS Introduction to Algoithms).
#include <iostream>
#include <vector>
using namespace std;
void MaxHeapify(vector<int>&, int, int, int);
void BuildMaxHeap(vector<int>&, int, int, int);
void heapSort(vector<int>&, int, int);
int main()
{
vector<int> A = {9,4,5,6,5,3,2,10,200,1}; // Random test data
cout << "UNSORTED:" << endl;
for(int i=0; i<A.size(); i++) cout << A[i] <<" ";
cout << endl;
heapSort(A, 0, A.size());
cout << "SORTED:" << endl;
for(int i=0; i<A.size(); i++) cout << A[i] <<" ";
cout << endl;
}
void MaxHeapify(vector<int>& A, int low, int i, int HeapSize)
{
int largest;
int l = 2*(i)+1; //LEFT(i)
int r = 2*(i)+ 1 + 1; // RIGHT(i)
if (l< HeapSize && A[low+l]>A[low+i])
{
largest = low+l;
}
else largest = low+i;
if (r< HeapSize && A[low+r]>A[largest])
{
largest = low+r;
}
if (largest != (low+i))
{
int temp = A[i+low];
A[i+low] = A[largest];
A[largest] = temp;
MaxHeapify(A, low, largest-low, HeapSize );
}
}
void BuildMaxHeap(vector<int>& A, int low, int high, int HeapSize)
{
for (int i=(high-low+1)/2-1; i>=0; i--)
{
MaxHeapify(A,low, i, HeapSize);
}
}
void heapSort(vector<int>& A, int low, int high)
{
int HeapSize = high-low+1;
BuildMaxHeap(A, low, high, HeapSize);
for (int i=high; i>low; i--)
{
int temp = A[i];
A[i] = A[low];
A[low] = temp;
HeapSize--;
MaxHeapify(A,low, 0, HeapSize);
}
}