-
Notifications
You must be signed in to change notification settings - Fork 1
/
HeapSort.cpp
109 lines (93 loc) · 2.35 KB
/
HeapSort.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "HeapSort.h"
HeapSort::HeapSort()
{
this->start = 0;
this->root = 0;
}
HeapSort::HeapSort(int arr_size)
{
this->start = (arr_size - 2) / 2;
this->root = start;
}
/**
* Implements the tick function for the HeapSort class.
*
* @param arr The input vector of integers to be sorted.
* @param i The current index of the array being sorted.
* @param n The size of the array being sorted.
* @param operation_counter The counter for tracking the number of operations performed.
*
* @return Returns true if the tick operation is successful, otherwise false.
*
* @throws None.
*/
bool HeapSort::tick(std::vector<int>& arr, int& i, int& n, int& operation_counter)
{
while (start >= 0)
{
while (2 * root + 1 <= arr.size() - 1)
{
int child = 2 * root + 1;
int swap = root;
if (child + 1 <= arr.size() - 1 && arr[swap] < arr[child + 1])
swap = child + 1;
if (arr[swap] < arr[child])
swap = child;
if (swap == root)
break;
std::swap(arr[root], arr[swap]);
root = swap;
operation_counter++;
i = swap;
return true;
}
start--;
root = start;
}
if (start == -1)
{
this->root = 0;
start = -2;
}
while (n > 0)
{
if (root == 0 || 2 * root + 1 > n)
{
std::swap(arr[n], arr[0]);
operation_counter++;
n--;
this->root = 0;
}
while (2 * root + 1 <= n)
{
int child = 2 * root + 1;
int swap = root;
if (child + 1 <= n && arr[swap] < arr[child + 1])
swap = child + 1;
if (arr[swap] < arr[child])
swap = child;
if (swap == root)
{
root = 0;
break;
}
std::swap(arr[root], arr[swap]);
operation_counter++;
root = swap;
i = swap;
return true;
}
}
return false;
}
/**
* Rebuilds the heap starting from the specified array size.
*
* @param arr_size the size of the array
*
* @throws ErrorType description of error
*/
void HeapSort::rebuild(int arr_size) {
this->start = (arr_size - 2) >> 1;
this->root = this->start;
}