-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeapSort.h
52 lines (42 loc) · 1.15 KB
/
HeapSort.h
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
#pragma once
#ifndef _HEAPSORT_H
#define _HEAPSORT_H
#include <iostream>
#include <string>
#include <vector>
#include <chrono>
using namespace std;
template <typename Comparable>
void percDown(vector<Comparable>& arr, int n, int i)
{
/*auto a = std::chrono::high_resolution_clock::now();*/
int largest = i; //largest as root
int l = 2 * i + 1; // left = 2*i + 1
int r = 2 * i + 2; // right = 2*i + 2
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
// If largest is not root
if (largest != i) {
swap(arr[i], arr[largest]);
percDown(arr, n, largest);
}
/*auto x = std::chrono::duration_cast<std::chrono::nanoseconds>
(std::chrono::high_resolution_clock::now() - a);
cout << "\nTime: " << x.count() << " nanosecond(s) Heap Sort" << "\n";*/
}
template <typename Comparable>
void heapSort(vector<Comparable>& arr)
{
int size = arr.size();
// Build heap (rearrange array)
for (int i = size / 2 - 1; i >= 0; i--)
percDown(arr, size, i);
// sort
for (int i = size - 1; i >= 0; i--) {
swap(arr[0], arr[i]);
percDown(arr, i, 0);
}
}
#endif