-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.h
More file actions
39 lines (34 loc) · 835 Bytes
/
QuickSort.h
File metadata and controls
39 lines (34 loc) · 835 Bytes
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
#pragma once
int Partition(unsigned *array, int start, int end) {
// 由于我们这里的数据为完全随机的数组,所以这里不需要实现随机快排
/*
若是要实现随机快排则只需添加如下代码
#include <ctime>
#include <cstdlib>
srand(time(0));
int index = rand() % (end-start)+start;
unsigned temp = array[index];
array[index] = array[end];
array[end] = temp;
*/
unsigned pivot = array[end];
int move = start;
for (int i = start; i < end; i++) {
if (array[i] < pivot) {
unsigned temp = array[i];
array[i] = array[move];
array[move] = temp;
move++;
}
}
array[end] = array[move];
array[move] = pivot;
return move;
}
void QuickSort(unsigned *array, int start, int end) {
if (start < end) {
int index = Partition(array, start, end);
QuickSort(array, start, index-1);
QuickSort(array, index + 1, end);
}
}