-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksort.cpp
More file actions
54 lines (45 loc) · 930 Bytes
/
Copy pathquicksort.cpp
File metadata and controls
54 lines (45 loc) · 930 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
54
#include <bits/stdc++.h>
using namespace std;
void quicksort(vector<int>& v, int left, int right) {
int i = left, j = right;
int pivot = v[left];
while (i <= j) {
while (v[i] < pivot)
{
i++;
}
while (v[j] > pivot)
{
j--;
}
if (i <= j) {
swap(v[i], v[j]);
i++;
j--;
}
}
if (left < j) {
quicksort(v, left, j);
}
if (i < right) {
quicksort(v, i, right);
}
}
int main() {
cout << "快速排序案例 输入格式:数组大小 + 需要排序的数组" << endl;
int n;
vector<int> num;
cin >> n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
num.push_back(x);
}
quicksort(num, 0, n - 1);
for (int i = 0; i < n; i++) {
cout << num[i] << ' ';
}
cout << endl;
system("pause");
return 0;
}