【hiho】37 二分·二分查找之k小数【二分查找-第k小数】

传送门:二分·二分查找之k小数

分析

分治法牛逼,参考快速排序中的分割函数,把分割的主元放出来,根据主元的下标,小的往左找,大的往右边找。

My AC Code

#include<iostream>
#include<stdlib.h>
 
using namespace std;
const int maxn=1e6+5;
 
int A[maxn];
void swap(int a[],int b[])
{
    int temp=*a;
    *a=*b;
    *b=temp;
}
 
 
int partition_k(int k,int a[],int first,int end)
{
    int i=first;
    int j=end;
    while(i<j)
    {
        while(i<j&&a[i]<a[j]) j--;
        if(i<j)
        {
            swap(a[i],a[j]);
            i++;
        }
        while(i<j&&a[i]<a[j]) i++;
        if(i<j)
        {
            swap(a[i],a[j]);
            j--;
        }
    }
    //return i;改写部分 
    if(k==j-first+1) return a[j];
    else if(k<j-first+1) return partition_k(k,a,first,j-1);
    else return partition_k(k-(j-first+1),a,j+1,end);
}
 
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    if(k>n||k<1)
    {
        cout<<-1;
        return 0;
    }
    for(int i=0;i<n;i++)
    {
        scanf("%d",&A[i]);
    }       
    cout<<partition_k(k,A,0,n-1);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/shengwang/p/9774931.html
今日推荐