QuickSelect Algorithm Understanding

前端 未结 6 1502
情深已故
情深已故 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条回答
  •  再見小時候
    2020-12-04 16:37

    btw, your code has a few bugs..

    int quickSelect(int items[], int first, int last, int k) {
        int pivot = partition(items, first, last);
        if (k < pivot-first+1) { //boundary was wrong
            return quickSelect(items, first, pivot, k);
        } else if (k > pivot-first+1) {//boundary was wrong
            return quickSelect(items, pivot+1, last, k-pivot);
        } else {
            return items[pivot];//index was wrong
        }
    }
    

提交回复
热议问题