-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquicksort.cpp
More file actions
63 lines (42 loc) · 771 Bytes
/
quicksort.cpp
File metadata and controls
63 lines (42 loc) · 771 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
#include <iostream>
template <typename T>
void QuickSort(T elems[], int begin, int end)
{
int i = begin, j = end, key = elems[begin+(end-begin)/2];
do {
while (elems[i] < key) {
i++;
}
while (elems[j] > key) {
j--;
}
if (i <= j) {
std::swap(elems[i++], elems[j--]);
}
} while (i < j);
if (j > begin) {
QuickSort(elems, begin, j);
}
if (i < end) {
QuickSort(elems, i, end);
}
}
int main(int argc, char *argv[])
{
int array[10];
srand(time(0));
for (auto &i : array) {
i = rand() % 100;
}
QuickSort(array, 0, 9);
for (auto i : array) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}