-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sorting.cpp
49 lines (46 loc) · 1.01 KB
/
Sorting.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
#include <vector>
#include <iostream>
using namespace std;
int Partition(vector<int> &resource, int low, int high)
{
int pivot = resource[high];
int i = low - 1;
for (int j = low; j < high - 1; j++)
{
if (resource[j] < pivot)
{
i++;
int temp = resource[i];
resource[i] = resource[i + 1];
resource[i + 1] = temp;
}
}
int temp = resource[i + 1];
resource[i + 1] = resource[high];
resource[high] = temp;
return i + 1;
}
void QuickSort(vector<int> &resource, int low, int high)
{
if (low < high)
{
int pivot = Partition(resource, low, high);
QuickSort(resource, low, pivot - 1);
QuickSort(resource, pivot + 1, high);
}
}
int main()
{
vector<int> v = {7, 9, 12, 31, 23, 1, 635, 456, 46, 1, 63};
for (int i : v)
{
cout << i << endl;
}
cout << "------------";
QuickSort(v, 0, v.size());
for (int i : v)
{
cout << i << endl;
}
return 0;
}