A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Selection Algorithms: Finding the kth Smallest Element

College Depth 88 in the knowledge graph ☐ I know this ☆ Set as goal
5topics build on this
487prerequisites beneath it
See this on the map →
QuicksortBig-O Notation and Complexity Analysis→→Maximum Flow: Network Flow Problems and Algorithms
selection algorithms linear-time

Core Idea

Quickselect finds the kth smallest element in O(n) average time by partitioning (like quicksort) but recursing on only the relevant partition. The Median-of-Medians algorithm guarantees O(n) worst-case time but with a large constant factor, making quickselect preferable in practice.

How It's Best Learned

Implement quickselect and compare to sorting then indexing. Observe average-case performance and analyze how the algorithm avoids sorting unused partitions. Study Median-of-Medians to understand worst-case linear-time selection.

Common Misconceptions

Explainer

The selection problem asks: given an unsorted array of n elements, find the kth smallest without sorting the entire array. The naive approach — sort first, then index — costs O(n log n). But sorting does far more work than necessary. You don't need every element in order; you only need to know which single element belongs at position k. Quickselect exploits this insight to solve the problem in O(n) average time.

Quickselect reuses the partitioning step from quicksort, which you already know. Pick a pivot, partition the array so that elements smaller than the pivot are on the left and larger elements are on the right, and the pivot lands in its final sorted position. Now compare k to the pivot's position. If k equals the pivot's index, you're done — the pivot is the answer. If k is smaller, the kth element must be in the left partition; if k is larger, it must be in the right partition. Here is the key difference from quicksort: you only recurse into one side. Quicksort recurses into both partitions to sort everything; quickselect throws away the irrelevant half. This single-sided recursion is what drops the average cost from O(n log n) to O(n) — each level of recursion processes roughly half as many elements as the previous one, so the total work forms a geometric series that sums to O(n).

The catch is the same as quicksort's: pivot choice matters. A consistently bad pivot (always the largest or smallest element) degrades quickselect to O(n²). Randomized pivot selection makes this astronomically unlikely in practice, giving expected O(n) performance. For guaranteed worst-case O(n), the Median-of-Medians algorithm chooses a pivot by dividing the array into groups of five, finding each group's median, and recursively selecting the median of those medians. This guarantees a balanced-enough partition — at least 30% of elements on each side — ensuring linear time. However, the constant factor is roughly 5-10x larger than randomized quickselect, so Median-of-Medians is primarily of theoretical importance. In practice, randomized quickselect is the standard choice.

Selection algorithms appear wherever you need order statistics without full sorting: finding the median for robust statistics, computing percentiles in streaming data, selecting the kth-closest point in nearest-neighbor searches, or partitioning data for load balancing across servers. The `std::nth_element` function in C++ and `numpy.partition` in Python both implement quickselect variants internally.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Variable Scope → Introduction to Classes → Objects and Instances → Methods and Attributes → Algorithm Design Basics → Asymptotic Notation: Big-O, Big-Omega, Big-Theta → Big-O Notation and Complexity Analysis → Time and Space Complexity → Binary Search → Divide and Conquer → Quicksort → Selection Algorithms: Finding the kth Smallest Element

Longest path: 89 steps · 487 total prerequisite topics

Prerequisites (2)

Leads To (1)