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 AnalysisMaximum 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 ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesBoolean AlgebraBoolean Type and Truth ValuesComparison Operators and Boolean TestsLogical Operators and Boolean AlgebraConditional StatementsDefining and Calling FunctionsFunctions: Decomposing ProblemsFunction Parameters and Argument PassingReturn ValuesVariable ScopeIntroduction to ClassesObjects and InstancesMethods and AttributesAlgorithm Design BasicsAsymptotic Notation: Big-O, Big-Omega, Big-ThetaBig-O Notation and Complexity AnalysisTime and Space ComplexityBinary SearchDivide and ConquerQuicksortSelection Algorithms: Finding the kth Smallest Element

Longest path: 89 steps · 487 total prerequisite topics

Prerequisites (2)

Leads To (1)