quickSelect C++

int partition(vector<int> &a,int l, int r){
    swap(a[l],a[l+rand()%(r-l+1)]);
    int j=l,pivot=a[l];
    for(int i=l+1;i<=r;i++){
        if(a[i]<pivot){
            swap(a[i],a[++j]);
        }
    }
    swap(a[l],a[j]);
    return j;
}
int quickSelect(vector<int>& arr, int k){
    int left=0,right=arr.size()-1;
    int m=k-1;
    while(left<=right){
        int index=partition(arr,left,right);
        if(index==m)    
            return arr[m];
        else if(index<m)
            left=index+1;
        else if(index>m)
            right=index-1;
    }
    return 0;
}

//写法二

class Solution {
public:
    int partition(vector<int>& nums, int le, int ri) {
        int pivot = nums[le + rand() % (ri - le + 1)];
        int lo = le - 1;
        int hi = ri + 1;
        while (1) {
            do lo++; while (nums[lo] < pivot);
            do hi--; while (nums[hi] > pivot);
            if (lo >= hi)   return hi;
            swap(nums[lo], nums[hi]);
        }
    }

    int quickSelect(vector<int>& nums, int le, int ri, int k) {
        if (le == ri)   return nums[le];
        int j = partition(nums, le, ri);
        if (k <= j) {
            return quickSelect(nums, le, j, k);
        } else {
            return quickSelect(nums, j + 1, ri, k);
        }
    }

    int findKthLargest(vector<int>& nums, int k) {
        return quickSelect(nums, 0, nums.size() - 1, nums.size() - k);
    }
};

库函数快速实现:

void nth_element( RandomIt first, RandomIt nth, RandomIt last );
void nth_element( RandomIt first, RandomIt nth, RandomIt last, Compare comp );

nth_element 是部分排序算法,它重排 [first, last) 中元素,使得nth前面元素都小于等于nth元素,nth后面元素都大于等于nth元素。
时间复杂度O(n)。
源代码使用内省选择 (Introselect)
内省选择算法首先调用快速选择算法,但如果其运算次数超过O(n)时,默认使用BFPRT算法,确保最坏运行时间为Theta(n)。

int nthOfArr(vector<int> &arr, int k){
    nth_element(arr.begin(), arr.begin() + k - 1, arr.end());
    return arr[k - 1];
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值