| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
手写快排,超时为什么?
//
// Created by Liao on 2018/7/11.
//
#include <stdio.h>
#include <stdlib.h>
int n;
int q[10005];
int partition(int *, int, int);
void quicksort(int *unsort, int low, int high){
int loc;
// printf("h");
if(low < high){
loc = partition(unsort, low, high);
quicksort(unsort, low, loc - 1);
quicksort(unsort, loc + 1, high);
}
}
int partition(int *unsort, int low, int high){
// printf("%d %d\n", low, high);
int pivot = unsort[low];
while(low < high){
while(low < high && unsort[high] > pivot) high --;
unsort[low] = unsort[high];
while(low < high &&unsort[low] < pivot) low ++;
unsort[high] = unsort[low];
}
// printf("%d %d\n", low, high);
unsort[low] = pivot;
return low;
}
int main(int argc, char *argv[]){
scanf("%d", &n);
int i = 0;
while(i < n){
scanf("%d", &q[i]);
i ++;
}
quicksort(q, 0, n - 1);
if (n % 2 == 0) printf("%d\n", 0.5 * (q[n/2] + q[n/2 - 1] ));
else printf("%d\n", q[(n-1)/2]);
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator