-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeapSort.cpp
112 lines (98 loc) · 2.87 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
110
111
112
#ifndef __HEAPSOT_CPP__
#define __HEAPSOT_CPP__
#include "Sort.h"
#include <stdlib.h>
size_t getNonLeafIndex(const size_t);
template<typename T>
bool HeapSwap(T* t_, const size_t n, const size_t index, size_t* spotLightIndex, const _FLAG flag, long long* step);
template<typename T>
bool Heapify(T* t_, const size_t n, const size_t index, const _FLAG flag, long long* step);
bool IsLeaf(const size_t n, const size_t index);
template<typename T>
long long Sort::HeapSort(T* t_, const size_t n, const _FLAG flag){ //n:1~n
int i;
long long step = 0;
size_t valiableN = n; //valiableN:1~
size_t nonLeafIndex = getNonLeafIndex(n); //nonLeafIndex:1~
T* tend = NULL;
if (nonLeafIndex == 0) return true;
for (i = nonLeafIndex; i > 0; --i){
if(!Heapify(t_, valiableN, i, flag, &step)) return -1;
}
for (valiableN = n - 1; valiableN >= 1; --valiableN){
tend = t_ + valiableN;
Sort::Swap(t_, tend);
++step;
if(!Heapify(t_, valiableN, 1, flag, &step)) return -1;
}
return step;
}
size_t getNonLeafIndex(const size_t n){
size_t pow = 0;
size_t num = 1;
while (n > num){
num <<= 1;
pow++;
};
if (pow == 0)
num = 2;
return ((num>>1) - 1);
}
template<typename T>
bool Heapify(T* t_, const size_t n, const size_t index, const _FLAG flag, long long* step){ //all :1~
bool swaped = false;
size_t spotLightIndex = 0; //1~
swaped = HeapSwap(t_, n, index, &spotLightIndex, flag, step);
(swaped) && (++(*step));
if ((swaped) && (!IsLeaf(n, spotLightIndex))){
return Heapify(t_, n, spotLightIndex, flag, step);
}
return true;
}
bool IsLeaf(const size_t n, const size_t index){
return (index > getNonLeafIndex(n));
}
template<typename T>
bool HeapSwap(T* t_, const size_t n, const size_t index, size_t* spotLightIndex, const _FLAG flag, long long* step){
T* leftLeaf = NULL;
T* rightLeaf = NULL;
T* spotLight = t_ + index - 1;
*spotLightIndex = index;
if (index > n)
return false;
if (n >= (index * 2) + 1){
leftLeaf = t_ + (index * 2 - 1);
rightLeaf = t_ + (index * 2);
if (((flag == POS) && (*leftLeaf >= *rightLeaf) && (*spotLight < *leftLeaf)) ||
((flag == NEG) && (*leftLeaf <= *rightLeaf) && (*spotLight > *leftLeaf)))
{
Sort::Swap(spotLight, leftLeaf);
(*step) += 1;
*spotLightIndex = index * 2;
return true;
}
else if (((flag == POS) && (*rightLeaf >= *leftLeaf) && (*spotLight < *rightLeaf)) ||
((flag == NEG) && (*rightLeaf <= *leftLeaf) && (*spotLight > *rightLeaf)))
{
Sort::Swap(spotLight, rightLeaf);
(*step) += 1;
*spotLightIndex = index * 2 + 1;
return true;
}
}
if (n == (index * 2)){
leftLeaf = t_ + (index * 2 - 1);
rightLeaf = NULL;
if ( ((flag == POS) && (*spotLight < *leftLeaf)) || ((flag == NEG) && (*spotLight > *leftLeaf)) ){
Sort::Swap(spotLight, leftLeaf);
(*step) += 1;
*spotLightIndex = index * 2;
return true;
}
}
if (n < (index * 2)){
return false;
}
return false;
}
#endif