-
Notifications
You must be signed in to change notification settings - Fork 2
/
POJ_2371_Questions and answers_快速排序.cpp
81 lines (76 loc) · 1.26 KB
/
POJ_2371_Questions and answers_快速排序.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <ctime>
using namespace std;
int partition(int *a,int low,int high)
{
if(low<high){
int pivot=a[high];
int p=low-1;
int tmp;
for(int i=low;i<high;++i){
if(a[i]<pivot){
p++;
tmp=a[i];
a[i]=a[p];
a[p]=tmp;
}
}
tmp=a[p+1];
a[p+1]=a[high];
a[high]=tmp;
return p+1;
}
return 0;
}
int random_partition(int *a,int low,int high)
{
int rnd=rand()%(high-low+1)+low;
int tmp=a[rnd];
a[rnd]=a[high];
a[high]=tmp;
return partition(a,low,high);
}
int random_select(int *a,int low,int high,int loc)//随机化中位数法
{
if(low==high){
return a[low];
}
int q=random_partition(a,low,high);
int k=q-low+1;
if(k==loc){
return a[q];
}else if(k<loc){
return random_select(a,q+1,high,loc-k);
}else{
return random_select(a,low,q-1,loc);
}
}
void quick_sort(int *a,int low,int high)
{
if(low<high){
int index=partition(a,low,high);
quick_sort(a,low,index-1);
quick_sort(a,index+1,high);
}
}
int main()
{
srand((unsigned)time(NULL));
int N;
cin>>N;
int *data=new int[N];
for(int i=0;i<N;++i){
cin>>data[i];
}
cin.ignore(3,'\n');
cin.ignore(3,'\n');
int k;
cin>>k;
int loc;
for(int i=0;i<k;++i){
cin>>loc;
int tmp=random_select(data,0,N-1,loc);
cout<<tmp<<endl;
}
delete [] data;
}