-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap_sort.cpp
More file actions
56 lines (47 loc) · 1.07 KB
/
heap_sort.cpp
File metadata and controls
56 lines (47 loc) · 1.07 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
#include "heap_sort.h"
namespace algorithm_ns
{
void HeapSort::sort()
{
if (nums_.empty())
{
return;
}
end_index_ = nums_.size();
_heap_init();
while(end_index_ != 1)
{
std::swap(nums_[0], nums_[end_index_-1]);
end_index_--;
_heap_adjust(0);
}
}
void
HeapSort::_heap_adjust(int i)
{
auto left_child = 2 * i + 1;
auto right_child = 2 * i + 2;
int max_index = i;
if (left_child < end_index_ && nums_[left_child] > nums_[max_index])
{
max_index = left_child;
}
if (right_child < end_index_ && nums_[right_child] > nums_[max_index])
{
max_index = right_child;
}
if (max_index != i)
{
std::swap(nums_[i], nums_[max_index]);
_heap_adjust(max_index);
}
}
void
HeapSort::_heap_init()
{
for (int i = nums_.size() / 2; i >= 0; i--)
{
_heap_adjust(i);
}
}
}