大佬求助!!!c用快排超时!!

P1923 【深基9.例4】求第 k 小的数

0523jayeup @ 2024-03-23 20:49:32

#include<stdio.h>

void quicksort(long int a[],int l,int r);
int sort(long int a[],int l,int r);

long int arr[5000005];
int main()
{
    long int i,j,n,index;
    scanf("%ld %ld",&n,&index);
    for(i=0;i<n;i++){
        scanf("%ld",&arr[i]);
    }
    quicksort(arr,0,n-1);
    printf("%ld",arr[index]);

    return 0;
}

int sort(long int a[],int l,int r)
{
    int prvit=a[l];
    while(l<r){
        while(a[r]>prvit&&l<r)r--;
        a[l]=a[r];
        while(a[l]<prvit&&l<r)l++;
        a[r]=a[l];
    }
    a[l]=prvit;
    return l;
}

void quicksort(long int a[],int l,int r)
{
    if(l<r){
        int prvit=sort(a,l,r);
        quicksort(a,l,prvit-1);
        quicksort(a,prvit+1,r);
    }
}

by XuYueming @ 2024-03-23 20:56:00

@0523jayeup 最劣 \Theta(n^2) 的代码超时正常


by Fish_Love_Water @ 2024-03-23 21:08:00

@0523jayeup 其实只是题目说了不能用nth_element的,假如你天生反骨话就会喜提AC...


by 0523jayeup @ 2024-03-23 21:20:36

@XuYueming 什么意思呀


|