求第K大的数还是有一个错误?

问题遇到的现象和发生背景

第 k 大的整数**

求 n 个整数中第 k(1≤k≤n) 大的整数。

输入格式
n 和 k
n 个整数

输出格式
第 k 大的整数

输入样例
10 3
2 5 -1 9 25 0 12 4 -7 12
输出样例
12

用代码块功能插入代码,请勿粘贴截图
#include 
#include 
using namespace std;
int quickselect(int num[],int start,int end,int k)
{
    int low=start;
    int high=end;
    int tmp=num[low];
    while(lowwhile(low--;
        num[low]=num[high];
        while(low=tmp) low++;
        num[high]=num[low];
    }
    num[high]=tmp;
    
    if(high==k-1) return tmp;//当前的枢轴点就是第K大的数 
    else if(high>k-1) return quickselect(num,start,end-1,k);
    else return quickselect(num,start+1,end,k);

}
int main()
{
    int n,k,e;
    cin>>n>>k;
    int a[100001];
    for(int i=0;i>a[i];
    }  
    e=quickselect(a,0,n-1,k);
    cout<return 0;
} 

运行结果及报错内容

img

这样改可以编译完全通过:


#include<iostream>
using namespace std;
#define N 100000000
int a[N]; 
int quickselect(int a[],int s,int t,int k)
{
    int i=s,j=t;
    int tmp;
    if(s<t)
    {
        tmp=a[s];
        while(i!=j)
        {
            while(j>i&&a[j]<=tmp)
            {
                j--;
            }
            a[i]=a[j];
            while(i<j&&a[i]>=tmp)
            {
                i++;
            }
            a[j]=a[i];
        }
        a[i]=tmp;
        if(k-1==i) return a[i];
        else if(k-1<i) return quickselect(a,s,i-1,k);
        else return quickselect(a,i+1,t,k);
    }
    else if(s==t&&s==k-1)
    return a[k-1];
}
int main()
{
    int n;
    int k;
    cin>>n>>k;
    for(int i=0;i<n;i++)
    cin>>a[i];
    cout<<quickselect(a,0,n-1,k);
}

段错误一般是数组越界了
看看n的范围,把数组开大一些,只要不超过10的八次方就没问题
有用记得采纳呐