Question Bank

Quickselect and Kth Element

Difficulty: Medium

Partition-based selection: kth smallest in expected `O(n)`, worst case `O(n^2)`, plus when to prefer a heap-based approach.

Question Bank
/

Quickselect and Kth Element

Quickselect and Kth Element

Partition-based selection: kth smallest in expected `O(n)`, worst case `O(n^2)`, plus when to prefer a heap-based approach.

Question Bank
Medium
Python
4 questions
quickselect
partitioning
algorithms
quiz

334 views

4

Implement quickselect to return the kth smallest element (k is 1-indexed) of an integer array using Lomuto partitioning.

Examples

Example 1:

Input: nums = [3, 1, 4, 1, 5, 9, 2, 6], k = 3
Output: 2
Explanation: Sorted view is [1, 1, 2, 3, 4, 5, 6, 9]. The 3rd smallest (1-indexed) is 2. Quickselect partitions around a (preferably random) pivot and recurses only into the side containing index k - 1. Expected O(n).