Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

手写快排,超时为什么?

Posted by 1120151880 at 2018-07-11 20:27:45 on Problem 2388
//
// 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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator