-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquick_sort.cpp
More file actions
56 lines (54 loc) · 1.04 KB
/
Copy pathquick_sort.cpp
File metadata and controls
56 lines (54 loc) · 1.04 KB
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
55
56
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#include<vector>
#include<algorithm>
using namespace std;
void quicksort(vector<int>& a,int i,int j);
void quicksort(vector<int>& array, int low, int high);
int main(){
vector<int> a;
int num;
cin>>num;
for(int i=0;i<num;i++){
int tmp=0;
cin>>tmp;
a.push_back(tmp);
}
quicksort(a,0,num-1);//开始下标 结束下标
for(int i=0;i<num;i++){
cout<<a[i]<<" ";
}
}
int quick_sort(vector<int>& a,int i,int j){
int key=a[i];
while(i<j){
while(i<j&&key<=a[j]){
j--;
}
if(i<j){
a[i]=a[j];
}
while(i<j&&key>=a[i]){
i++;
}
if(i<j){
a[j]=a[i];
}
}
a[i]=key;
return i;
}
void quicksort(vector<int>& array, int low, int high) {
// 开始默认基准为 low
if (low < high) {
// 分段位置下标
int standard = quick_sort(array, low, high);
// 递归调用排序
// 左边排序
quicksort(array, low, standard - 1);
// 右边排序
quicksort(array, standard + 1, high);
}
}