Skip to content

Commit d09542f

Browse files
committed
update index heap
1 parent d9a7ee7 commit d09542f

File tree

4 files changed

+340
-0
lines changed

4 files changed

+340
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,7 @@
7171
- 二叉堆
7272
> 堆 是一个完全二叉树
7373
> ![完全二叉树](./static/complete-bin-tree.png)
74+
75+
- 排序算法总结
76+
> ![排序算法总结](./static/sort-solution.png)
77+
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
//
2+
// Created by 3zz.
3+
//
4+
5+
#ifndef INC_08_INDEX_HEAP_SORTTESTHELPER_H
6+
#define INC_08_INDEX_HEAP_SORTTESTHELPER_H
7+
8+
#include <iostream>
9+
#include <algorithm>
10+
#include <string>
11+
#include <ctime>
12+
#include <cassert>
13+
#include <string>
14+
15+
using namespace std;
16+
17+
namespace SortTestHelper
18+
{
19+
20+
// 生成有n个元素的随机数组,每个元素的随机范围为[rangeL, rangeR]
21+
int *generateRandomArray(int n, int range_l, int range_r)
22+
{
23+
24+
int *arr = new int[n];
25+
26+
srand(time(NULL));
27+
for (int i = 0; i < n; i++)
28+
arr[i] = rand() % (range_r - range_l + 1) + range_l;
29+
return arr;
30+
}
31+
32+
// 生成一个近乎有序的数组
33+
// 首先生成一个含有[0...n-1]的完全有序数组, 之后随机交换swapTimes对数据
34+
// swapTimes定义了数组的无序程度
35+
int *generateNearlyOrderedArray(int n, int swapTimes)
36+
{
37+
38+
int *arr = new int[n];
39+
for (int i = 0; i < n; i++)
40+
arr[i] = i;
41+
42+
srand(time(NULL));
43+
for (int i = 0; i < swapTimes; i++)
44+
{
45+
int posx = rand() % n;
46+
int posy = rand() % n;
47+
swap(arr[posx], arr[posy]);
48+
}
49+
50+
return arr;
51+
}
52+
53+
// 拷贝整型数组a中的所有元素到一个新的数组, 并返回新的数组
54+
int *copyIntArray(int a[], int n)
55+
{
56+
57+
int *arr = new int[n];
58+
//* 在VS中, copy函数被认为是不安全的, 请大家手动写一遍for循环:)
59+
copy(a, a + n, arr);
60+
return arr;
61+
}
62+
63+
// 打印arr数组的所有内容
64+
template <typename T>
65+
void printArray(T arr[], int n)
66+
{
67+
68+
for (int i = 0; i < n; i++)
69+
cout << arr[i] << " ";
70+
cout << endl;
71+
72+
return;
73+
}
74+
75+
// 判断arr数组是否有序
76+
template <typename T>
77+
bool isSorted(T arr[], int n)
78+
{
79+
80+
for (int i = 0; i < n - 1; i++)
81+
if (arr[i] > arr[i + 1])
82+
return false;
83+
84+
return true;
85+
}
86+
87+
// 测试sort排序算法排序arr数组所得到结果的正确性和算法运行时间
88+
// 将算法的运行时间打印在控制台上
89+
template <typename T>
90+
void testSort(const string &sortName, void (*sort)(T[], int), T arr[], int n)
91+
{
92+
93+
clock_t startTime = clock();
94+
sort(arr, n);
95+
clock_t endTime = clock();
96+
cout << sortName << " : " << double(endTime - startTime) / CLOCKS_PER_SEC << " s" << endl;
97+
98+
assert(isSorted(arr, n));
99+
100+
return;
101+
}
102+
103+
// 测试sort排序算法排序arr数组所得到结果的正确性和算法运行时间
104+
// 将算法的运行时间以double类型返回, 单位为秒(s)
105+
template <typename T>
106+
double testSort(void (*sort)(T[], int), T arr[], int n)
107+
{
108+
109+
clock_t startTime = clock();
110+
sort(arr, n);
111+
clock_t endTime = clock();
112+
113+
assert(isSorted(arr, n));
114+
115+
return double(endTime - startTime) / CLOCKS_PER_SEC;
116+
}
117+
118+
}; // namespace SortTestHelper
119+
120+
#endif //INC_08_INDEX_HEAP_SORTTESTHELPER_H

heap-sort/06-Index-Heap/main.cpp

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
#include <iostream>
2+
#include <cassert>
3+
#include "SortTestHelper.h"
4+
5+
using namespace std;
6+
7+
// 最大索引堆
8+
template <typename Item>
9+
class IndexMaxHeap
10+
{
11+
12+
private:
13+
Item *data; // 最大索引堆中的数据
14+
int *indexes; // 最大索引堆中的索引
15+
16+
int count;
17+
int capacity;
18+
19+
// 索引堆中, 数据之间的比较根据data的大小进行比较, 但实际操作的是索引
20+
void shiftUp(int k)
21+
{
22+
23+
while (k > 1 && data[indexes[k / 2]] < data[indexes[k]])
24+
{
25+
swap(indexes[k / 2], indexes[k]);
26+
k /= 2;
27+
}
28+
}
29+
30+
// 索引堆中, 数据之间的比较根据data的大小进行比较, 但实际操作的是索引
31+
void shiftDown(int k)
32+
{
33+
34+
while (2 * k <= count)
35+
{
36+
int j = 2 * k;
37+
if (j + 1 <= count && data[indexes[j + 1]] > data[indexes[j]])
38+
j += 1;
39+
40+
if (data[indexes[k]] >= data[indexes[j]])
41+
break;
42+
43+
swap(indexes[k], indexes[j]);
44+
k = j;
45+
}
46+
}
47+
48+
public:
49+
// 构造函数, 构造一个空的索引堆, 可容纳capacity个元素
50+
IndexMaxHeap(int capacity)
51+
{
52+
53+
data = new Item[capacity + 1];
54+
indexes = new int[capacity + 1];
55+
56+
count = 0;
57+
this->capacity = capacity;
58+
}
59+
60+
~IndexMaxHeap()
61+
{
62+
delete[] data;
63+
delete[] indexes;
64+
}
65+
66+
// 返回索引堆中的元素个数
67+
int size()
68+
{
69+
return count;
70+
}
71+
72+
// 返回一个布尔值, 表示索引堆中是否为空
73+
bool isEmpty()
74+
{
75+
return count == 0;
76+
}
77+
78+
// 向最大索引堆中插入一个新的元素, 新元素的索引为i, 元素为item
79+
// 传入的i对用户而言,是从0索引的
80+
void insert(int i, Item item)
81+
{
82+
assert(count + 1 <= capacity);
83+
assert(i + 1 >= 1 && i + 1 <= capacity);
84+
85+
i += 1;
86+
data[i] = item;
87+
indexes[count + 1] = i;
88+
count++;
89+
90+
shiftUp(count);
91+
}
92+
93+
// 从最大索引堆中取出堆顶元素, 即索引堆中所存储的最大数据
94+
Item extractMax()
95+
{
96+
assert(count > 0);
97+
98+
Item ret = data[indexes[1]];
99+
swap(indexes[1], indexes[count]);
100+
count--;
101+
shiftDown(1);
102+
return ret;
103+
}
104+
105+
// 从最大索引堆中取出堆顶元素的索引
106+
int extractMaxIndex()
107+
{
108+
assert(count > 0);
109+
110+
int ret = indexes[1] - 1;
111+
swap(indexes[1], indexes[count]);
112+
count--;
113+
shiftDown(1);
114+
return ret;
115+
}
116+
117+
// 获取最大索引堆中的堆顶元素
118+
Item getMax()
119+
{
120+
assert(count > 0);
121+
return data[indexes[1]];
122+
}
123+
124+
// 获取最大索引堆中的堆顶元素的索引
125+
int getMaxIndex()
126+
{
127+
assert(count > 0);
128+
return indexes[1] - 1;
129+
}
130+
131+
// 获取最大索引堆中索引为i的元素
132+
Item getItem(int i)
133+
{
134+
assert(i + 1 >= 1 && i + 1 <= capacity);
135+
return data[i + 1];
136+
}
137+
138+
// 将最大索引堆中索引为i的元素修改为newItem
139+
void change(int i, Item newItem)
140+
{
141+
142+
i += 1;
143+
data[i] = newItem;
144+
145+
// 找到indexes[j] = i, j表示data[i]在堆中的位置
146+
// 之后shiftUp(j), 再shiftDown(j)
147+
for (int j = 1; j <= count; j++)
148+
if (indexes[j] == i)
149+
{
150+
shiftUp(j);
151+
shiftDown(j);
152+
return;
153+
}
154+
}
155+
156+
// 测试索引堆中的索引数组index
157+
// 注意:这个测试在向堆中插入元素以后, 不进行extract操作有效
158+
bool testIndexes()
159+
{
160+
161+
int *copyIndexes = new int[count + 1];
162+
163+
for (int i = 0; i <= count; i++)
164+
copyIndexes[i] = indexes[i];
165+
166+
copyIndexes[0] = 0;
167+
std::sort(copyIndexes, copyIndexes + count + 1);
168+
169+
// 在对索引堆中的索引进行排序后, 应该正好是1...count这count个索引
170+
bool res = true;
171+
for (int i = 1; i <= count; i++)
172+
if (copyIndexes[i - 1] + 1 != copyIndexes[i])
173+
{
174+
res = false;
175+
break;
176+
}
177+
178+
delete[] copyIndexes;
179+
180+
if (!res)
181+
{
182+
cout << "Error!" << endl;
183+
return false;
184+
}
185+
186+
return true;
187+
}
188+
};
189+
190+
// 使用最大索引堆进行堆排序, 来验证我们的最大索引堆的正确性
191+
// 最大索引堆的主要作用不是用于排序, 我们在这里使用排序只是为了验证我们的最大索引堆实现的正确性
192+
// 在后续的图论中, 无论是最小生成树算法, 还是最短路径算法, 我们都需要使用索引堆进行优化:)
193+
template <typename T>
194+
void heapSortUsingIndexMaxHeap(T arr[], int n)
195+
{
196+
197+
IndexMaxHeap<T> indexMaxHeap = IndexMaxHeap<T>(n);
198+
for (int i = 0; i < n; i++)
199+
indexMaxHeap.insert(i, arr[i]);
200+
assert(indexMaxHeap.testIndexes());
201+
202+
for (int i = n - 1; i >= 0; i--)
203+
arr[i] = indexMaxHeap.extractMax();
204+
}
205+
206+
int main()
207+
{
208+
209+
int n = 1000000;
210+
211+
int *arr = SortTestHelper::generateRandomArray(n, 0, n);
212+
SortTestHelper::testSort("Heap Sort Using Index-Max-Heap", heapSortUsingIndexMaxHeap, arr, n);
213+
delete[] arr;
214+
215+
return 0;
216+
}

static/sort-solution.png

237 KB
Loading

0 commit comments

Comments
 (0)