Algo-Sim › Sorting & Divide and Conquer

Kth Largest Element (Quickselect)

Medium · Sorting & Divide and Conquer · step through it in Java, Python and C++ on your own input

Find the kth largest without sorting: partition, then continue only into the side holding index n − k. Average O(n).

Problem

Return the kth largest element of nums (counting duplicates) without fully sorting it: partition around a pivot and continue only into the side that must contain the answer.

Constraints

Edge cases to test

Follow-up questions

How do you make the worst case linear?
Median of medians picks a pivot that splits at least 30/70: O(n) worst case, slower constants.
Heap or quickselect?
A size-k heap is O(n log k) and streams; quickselect is O(n) average but needs the whole array and reorders it.

Solution

Java

public int findKthLargest(int[] nums, int k) {
    int target = nums.length - k, lo = 0, hi = nums.length - 1;
    while (true) {
        int p = partition(nums, lo, hi);
        if (p == target) return nums[p];
        if (p < target) lo = p + 1;
        else hi = p - 1;
    }
}

// Lomuto partition: pivot = nums[hi] ends at its sorted index.
private int partition(int[] nums, int lo, int hi) {
    int pivot = nums[hi], i = lo;
    for (int j = lo; j < hi; j++) {
        if (nums[j] < pivot) {
            int t = nums[i]; nums[i] = nums[j]; nums[j] = t;
            i++;
        }
    }
    int t = nums[i]; nums[i] = nums[hi]; nums[hi] = t;
    return i;
}

Python

def find_kth_largest(nums, k):
    target, lo, hi = len(nums) - k, 0, len(nums) - 1
    while True:
        p = partition(nums, lo, hi)
        if p == target:
            return nums[p]
        if p < target:
            lo = p + 1
        else:
            hi = p - 1


# Lomuto partition: pivot = nums[hi] ends at its sorted index.
def partition(nums, lo, hi):
    pivot, i = nums[hi], lo
    for j in range(lo, hi):
        if nums[j] < pivot:
            nums[i], nums[j] = nums[j], nums[i]
            i += 1
    nums[i], nums[hi] = nums[hi], nums[i]
    return i

C++

#include <bits/stdc++.h>
using namespace std;

class Solution {
  public:
    int findKthLargest(vector<int> &nums, int k) {
        int target = int(nums.size()) - k, lo = 0, hi = int(nums.size()) - 1;
        while (true) {
            int p = partition(nums, lo, hi);
            if (p == target)
                return nums[p];
            if (p < target)
                lo = p + 1;
            else
                hi = p - 1;
        }
    }

    // Lomuto partition: pivot = nums[hi] ends at its sorted index.
    int partition(vector<int> &nums, int lo, int hi) {
        int pivot = nums[hi], i = lo;
        for (int j = lo; j < hi; j++) {
            if (nums[j] < pivot) {
                int t = nums[i];
                nums[i] = nums[j];
                nums[j] = t;
                i++;
            }
        }
        int t = nums[i];
        nums[i] = nums[hi];
        nums[hi] = t;
        return i;
    }
};

More Sorting & Divide and Conquer problems