快排与TopK 问题

快排与TopK 问题都可以用partition解决,所以这里将两者放在一起进行总结

topK 问题

#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
class Solution{
    public:
        int partition (int left, int right, vector<int>& nums){
            int index = left;
            int key = nums[right];
            for(int i = left; i <= right; i++){
                if (nums[i] > key){
                    swap(nums[index++], nums[i]);
                }
            }
            // cout<< "ss"<<index<<endl;
            swap(nums[index], nums[right]);
            return index; //  返回一个索引,这个索引前面的数字都比他大
            // 注意啊这个index 返回值可以是[left, right]
        } 
        void top_k( int left, int right, vector<int>& nums, int k ){
            int index = partition(left, right, nums);
            if (index == k){
                return;
            }else if (index > k){ // index 太大了 
                top_k(left, index-1 ,nums,k);
            }else{
                top_k(index+1, right, nums, k);
            }
        }

        vector<int> top_k (vector<int>& nums,int k){ // 这些从参数对问题规模的描述不是很贴切
            int left = 0, right = nums.size()-1;
            top_k(left, right, nums, k);
            vector<int> res;
            for(int i = 0; i < k; i++){
                res.push_back(nums[i]);
            }
            return res;
        }   
};

int main(){
    vector<int> array = {1,2,3,2,3,1,2,3,4};
    Solution ss;
    ss.partition(0, array.size()-1,array);
    for(auto& arr : array){
        cout<< arr <<"    ";
    }
    cout<<endl;
    vector<int> res = ss.top_k(array, 5);
    int n = res.size();
    for(int i = 0; i < n;i++){
        cout<< res[i]<<"  ";
    }
    system("pause");
}

快排

#include<vector>
#include<iostream>
using namespace std;
class Solution{
    public:
        vector<int> array_sort(vector<int>& nums){
            int left = 0,  right = nums.size()-1;
            quick_sort(left, right, nums);
            return nums;
        }
        void quick_sort(int left, int right, vector<int>& nums){
            if (left >= right)
                return;
            int key = nums[left];
            int index = left;
            for(int i = left+1; i <= right; i++){
                if (nums[i] >= key){
                    swap(nums[i], nums[++left]);
                }
            }
            swap(nums[left] , nums[index]);
            quick_sort(index,  left-1, nums);
            quick_sort(left+1,  right, nums);
        }

        // void quick_sort(int left, int right, vector<int>& nums){
        //     if (left >= right)
        //         return;
        //     int key = nums[right];
        //     int index = right;
        //     for(int i = right-1; i >= left; i--){
        //         if (nums[i] >= key){
        //             swap(nums[i], nums[--right]);
        //         }
        //     }
        //     swap(nums[right] , nums[index]);
        //     quick_sort(left,  right-1, nums);
        //     quick_sort(right+1,  index, nums);
        // } 
};

int main(){
    vector<int> array = { 1,2,3,5,2,2,4,7,3,4,4,7,5};
    Solution ss;
    ss.array_sort(array);
    int n = array.size();
    for(int i = 0; i < n;i++){
        cout<< array[i]<<"  ";
    }
    system("pause");
}
上一篇:收藏基础算法刷题好的评论


下一篇:堆的创建、优先队列、topk、堆排序C语言实现