Skip to content

Commit d0cf2bd

Browse files
committed
Create Heap structure, write some sort algorithms.
1 parent 135631d commit d0cf2bd

File tree

21 files changed

+799
-40
lines changed

21 files changed

+799
-40
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Books/*
1+
doc

Heap/Heap/Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
DEPEND := heap.h heap.cpp \
2+
../../Vector/vector.h ../../Vector/vector.cpp \
3+
main.cpp
4+
5+
INCLUDE := -I. -I../../Vector
6+
7+
all: main
8+
9+
main: $(DEPEND)
10+
g++ -std=c++11 main.cpp $(INCLUDE) -g -o main
11+
12+
clean:
13+
del main.exe
14+
15+
run: main
16+
.\\main.exe
17+
18+
gdb: main
19+
gdb -q --interpreter=mi --args main.exe

Heap/Heap/heap.cpp

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
#ifdef HEAP_H
2+
3+
template<class DataType>
4+
int Heap<DataType>::lchild(int i)
5+
{
6+
return 2 * i + 1;
7+
}
8+
9+
template<class DataType>
10+
int Heap<DataType>::rchild(int i)
11+
{
12+
return 2 * i + 2;
13+
}
14+
15+
template<class DataType>
16+
int Heap<DataType>::parent(int i)
17+
{
18+
return ceil(0.5 * i - 1);
19+
}
20+
21+
template<class DataType>
22+
int Heap<DataType>::depth(int i)
23+
{
24+
return ceil(log2(i+2)) - 1;
25+
}
26+
27+
template<class DataType>
28+
void Heap<DataType>::up_percolate(Vector<DataType>& v, int lower, int upper)
29+
{
30+
int n = upper - lower;
31+
int i = n - 1;
32+
while(parent(i) >= 0 && v[parent(i) + lower] < v[i + lower])
33+
{
34+
i = parent(i);
35+
(rchild(i) >= n || v[lchild(i) + lower] >= v[rchild(i) + lower]) ?
36+
swap(v[i + lower], v[lchild(i) + lower]) : swap(v[i + lower], v[rchild(i) + lower]);
37+
}
38+
}
39+
40+
template<class DataType>
41+
void Heap<DataType>::down_percolate(Vector<DataType>& v, int lower, int upper, int i)
42+
{
43+
int n = upper - lower;
44+
i -= lower;
45+
while(true)
46+
{
47+
if(lchild(i) >= n)
48+
{
49+
return;
50+
}
51+
52+
(rchild(i) >= n || v[lchild(i) + lower] >= v[rchild(i) + lower]) ?
53+
i = lchild(i) : i = rchild(i);
54+
55+
if(v[i + lower] <= v[parent(i) + lower])
56+
{
57+
return;
58+
}
59+
60+
swap(v[i + lower], v[parent(i) + lower]);
61+
}
62+
}
63+
64+
template<class DataType>
65+
void Heap<DataType>::up_percolate()
66+
{
67+
int i = v.size()-1;
68+
while(parent(i) >= 0 && v[parent(i)] < v[i])
69+
{
70+
i = parent(i);
71+
(rchild(i) >= size() || v[lchild(i)] >= v[rchild(i)]) ?
72+
swap(v[i], v[lchild(i)]) : swap(v[i], v[rchild(i)]);
73+
}
74+
}
75+
76+
template<class DataType>
77+
void Heap<DataType>::down_percolate(int i)
78+
{
79+
while(true)
80+
{
81+
if(lchild(i) >= size())
82+
{
83+
return;
84+
}
85+
86+
(rchild(i) >= size() || v[lchild(i)] >= v[rchild(i)]) ?
87+
i = lchild(i) : i = rchild(i);
88+
89+
if(v[i] <= v[parent(i)])
90+
{
91+
return;
92+
}
93+
94+
swap(v[i], v[parent(i)]);
95+
}
96+
}
97+
98+
template<class DataType>
99+
DataType Heap<DataType>::top()
100+
{
101+
return v[0];
102+
}
103+
104+
template<class DataType>
105+
DataType Heap<DataType>::pop()
106+
{
107+
if(empty())
108+
{
109+
cout << "Error in 'DataType Heap<DataType>::pop()'" << endl
110+
<< "The heap is empty, nothing to pop!" << endl;
111+
exit(-1);
112+
}
113+
114+
if(size() == 1)
115+
{
116+
return v.pop_back();
117+
}
118+
119+
DataType x = v[0];
120+
DataType v0 = v.pop_back();
121+
v[0] = v0;
122+
down_percolate(0);
123+
124+
return x;
125+
}
126+
127+
template<class DataType>
128+
void Heap<DataType>::insert(const DataType& x)
129+
{
130+
v.push_back(x);
131+
up_percolate();
132+
}
133+
134+
// 当把向量直接当堆用时,可以使用这些接口
135+
template<class DataType>
136+
bool Heap<DataType>::isheap(const Vector<DataType>& v, int lower, int upper)
137+
{
138+
v.check_limit(lower, upper, "bool Heap<DataType>::isheap(const Vector<DataType>& v, int lower, int upper)");
139+
140+
int n = upper - lower;
141+
for(int i = 0; i < n; i++)
142+
{
143+
if( (lchild(i)+lower < upper && v[lchild(i)+lower] > v[i+lower]) ||
144+
(rchild(i)+lower < upper && v[rchild(i)+lower] > v[i+lower]) )
145+
{
146+
return false;
147+
}
148+
}
149+
150+
return true;
151+
}
152+
153+
template<class DataType>
154+
void Heap<DataType>::heapify(Vector<DataType>& v, int lower, int upper)
155+
{
156+
v.check_limit(lower, upper, "void Heap<DataType>::heapify(Vector<DataType>& v, int lower, int upper)");
157+
158+
for(int i = upper - 1; i >= lower; i--)
159+
{
160+
down_percolate(v, lower, upper, i);
161+
}
162+
}
163+
164+
template<class DataType>
165+
DataType Heap<DataType>::pop(Vector<DataType>& v, int lower, int& upper)
166+
{
167+
v.check_limit(lower, upper, "DataType Heap<DataType>::pop(Vector<DataType>& v, int lower, int& upper)");
168+
169+
if(upper - lower == 1)
170+
{
171+
upper--;
172+
return v[lower];
173+
}
174+
175+
DataType x = v[lower];
176+
v[lower] = v[upper-1];
177+
upper--;
178+
down_percolate(v, lower, upper, lower);
179+
180+
return x;
181+
}
182+
183+
template<class DataType>
184+
DataType Heap<DataType>::pop(Vector<DataType>& v)
185+
{
186+
if(v.empty())
187+
{
188+
cout << "Error in 'DataType Heap<DataType>::pop()'" << endl
189+
<< "The heap is empty, nothing to pop!" << endl;
190+
exit(-1);
191+
}
192+
193+
if(v.size() == 1)
194+
{
195+
return v.pop_back();
196+
}
197+
198+
DataType x = v[0];
199+
DataType v0 = v.pop_back();
200+
v[0] = v0;
201+
down_percolate(v, 0, v.size(), 0);
202+
203+
return x;
204+
}
205+
206+
template<class DataType>
207+
void Heap<DataType>::insert(Vector<DataType>& v, int lower, int& upper, const DataType& x)
208+
{
209+
v.check_limit(lower, upper, "void Heap<DataType>::insert(Vector<DataType>& v, int lower, int& upper, const DataType& x)");
210+
211+
v.insert(upper, x);
212+
upper++;
213+
up_percolate(v, lower, upper);
214+
}
215+
216+
template<class DataType>
217+
void Heap<DataType>::insert(Vector<DataType>& v, int lower, int& upper)
218+
{
219+
v.check_limit(lower, upper, "void Heap<DataType>::insert(Vector<DataType>& v, int lower, int& upper)");
220+
221+
upper++;
222+
up_percolate(v, lower, upper);
223+
}
224+
225+
template<class DataType>
226+
void Heap<DataType>::insert(Vector<DataType>& v, const DataType& x)
227+
{
228+
v.push_back(x);
229+
up_percolate(v, 0, v.size());
230+
}
231+
232+
#endif

Heap/Heap/heap.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#ifndef HEAP_H
2+
#define HEAP_H
3+
4+
#include <vector.h>
5+
#include <cmath>
6+
7+
template<class DataType>
8+
class Heap
9+
{
10+
public:
11+
Vector<DataType> v;
12+
13+
private:
14+
static int lchild(int i);
15+
static int rchild(int i);
16+
static int parent(int i);
17+
static int depth(int i);
18+
19+
void up_percolate();
20+
void down_percolate(int i);
21+
22+
static void up_percolate(Vector<DataType>& v, int lower, int upper);
23+
static void down_percolate(Vector<DataType>& v, int lower, int upper, int i);
24+
25+
public:
26+
Heap(){};
27+
Heap(const Vector<DataType>& vec){v = vec; heapify(v, 0, v.size());};
28+
29+
void clear(){v.clear();}
30+
int size()const{return v.size();}
31+
bool empty()const{return v.empty();}
32+
33+
DataType top();
34+
DataType pop();
35+
void insert(const DataType& x);
36+
37+
// 当把向量直接当堆用时,可以使用这些接口
38+
static bool isheap(const Vector<DataType>& v, int lower, int upper);
39+
static bool isheap(const Vector<DataType>& v){return isheap(v, 0, v.size());}
40+
41+
static void heapify(Vector<DataType>& v, int lower, int upper);
42+
static void heapify(Vector<DataType>& v){heapify(v, 0, v.size());}
43+
44+
static DataType top(Vector<DataType>& v, int lower, int upper){return v[lower];}
45+
static DataType top(Vector<DataType>& v){return v[0];}
46+
47+
static DataType pop(Vector<DataType>& v, int lower, int& upper);
48+
static DataType pop(Vector<DataType>& v);
49+
50+
static void insert(Vector<DataType>& v, int lower, int& upper, const DataType& x);
51+
static void insert(Vector<DataType>& v, const DataType& x);
52+
static void insert(Vector<DataType>& v, int lower, int& upper);
53+
};
54+
55+
#include <heap.cpp>
56+
#endif

Heap/Heap/main.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <heap.h>
2+
3+
int main()
4+
{
5+
Vector<int> v = int_rand(0, 10, 20);
6+
Heap<int>::heapify(v);
7+
cout << Heap<int>::isheap(v) << endl;
8+
cout << v << endl;
9+
Heap<int>::insert(v, 7);
10+
cout << v << endl;
11+
cout << Heap<int>::isheap(v) << endl;
12+
return 0;
13+
}

Heap/Heap/main.exe

91.9 KB
Binary file not shown.

List/List/list.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ class List
6161
int length = 0;
6262

6363
public:
64-
List<DataType>();
65-
List<DataType>(int n);
66-
List<DataType>(int n, DataType element);
67-
List<DataType>(const List<DataType>& list);
68-
~List<DataType>();
64+
List();
65+
List(int n);
66+
List(int n, DataType element);
67+
List(const List<DataType>& list);
68+
~List();
6969
List<DataType>& operator =(const List<DataType>& list);
7070
void clear();
7171
int size()const;

Sort/Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
DEPEND := sort.h sort.cpp \
2+
../Vector/vector.h ../Vector/vector.cpp \
3+
../List/List/list.h ../List/List/list.cpp \
4+
../Heap/Heap/heap.h ../Heap/Heap/heap.cpp \
5+
main.cpp
6+
7+
INCLUDE := -I. -I../Vector -I../List/List -I../Heap/Heap
8+
9+
all: main
10+
11+
main: $(DEPEND)
12+
g++ -std=c++11 main.cpp $(INCLUDE) -g -o main
13+
14+
clean:
15+
del main.exe
16+
17+
run: main
18+
.\\main.exe
19+
20+
gdb: main
21+
gdb -q --interpreter=mi --args main.exe

Sort/main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <sort.h>
2+
3+
int main()
4+
{
5+
Vector<int> v = int_rand(0, 20, 20);
6+
cout << "Before sort: " << v << endl;
7+
count_sort(v);
8+
cout << "After sort: " << v << endl;
9+
10+
return 0;
11+
}

Sort/main.exe

83.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)