QuickSelect Algorithm Understanding

前端 未结 6 1517
情深已故
情深已故 2020-12-04 15:58

I\'ve been poring over various tutorials and articles that discuss quicksort and quickselect, however my understanding of them is still shaky.

Given this code struct

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 16:27

    int partition(vector &vec, int left, int right, int pivotIndex)
    {
            int pivot = vec[pivotIndex];
            int partitionIndex = left;
    
            swap(vec[pivotIndex],vec[right]);
            for(int i=left; i < right; i++) {
                    if(vec[i] &vec, int left, int right, int k)
    {
            int pivotIndex;
            if (right == left) {
                    return vec[left];
            }
            pivotIndex = left + rand() % (right-left);
    
            pivotIndex = partition(vec,left,right,pivotIndex);
            if (pivotIndex == k) {
                    return vec[k];
            } else if(k

提交回复
热议问题