绫韵华音 @ 2021-10-11 20:44:41
void my_sort(int L, int R){
if(L > R)
return ;
for(int i = L; i <= R; i ++)
b[i] = a[i];
int x = a[(L + R) >> 1], l = L - 1, r = R + 1;
for(int i = L; i <= R; i ++)
if(b[i] < x)
a[++ l] = b[i];
else if(b[i] > x)
a[-- r] = b[i];
for(int i = l + 1; i <= r - 1; i ++)
a[i] = x;
if(l + 1 <= k && k <= r - 1){
cout << x;
return ;
}
if(k <= l)
my_sort(L, l);
else
my_sort(r, R);
return ;
}
用了快排的方法,把区间L~R的数分成两部分,左小右大。 下面是我用同样方法写的快排,还过了排序模板P1177
void my_sort(int L, int R){
if(L >= R)
return ;
for(int i = L; i <= R; i ++)
b[i] = a[i];
int x = a[(L + R) >> 1], l = L - 1, r = R + 1;
for(int i = L; i <= R; i ++)
if(b[i] < x)
a[++ l] = b[i];
else if(b[i] > x)
a[-- r] = b[i];
for(int i = l + 1; i <= r - 1; i ++)
a[i] = x;
my_sort(L, l);
my_sort(r, R);
return ;
}
萌新求助QAQ
by 绫韵华音 @ 2021-10-11 21:27:31
又是自己解决了,最小是第0小,读题不仔细,自己两行泪QAQ