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

Selection Sort Algorithm

College Depth 82 in the knowledge graph ☐ I know this ☆ Set as goal
8topics build on this
398prerequisites beneath it
See this on the map →
Arrays and ListsAlgorithm Design Basics→→Radix Sort Algorithm
sorting selection-sort comparison-sort in-place

Core Idea

Selection sort divides the array into sorted and unsorted regions, repeatedly finding the minimum in the unsorted region and swapping it to the end of the sorted region. It is O(n²) in all cases (best, average, worst), makes exactly n−1 swaps (minimal), and requires O(1) space. It is useful when memory writes are expensive.

How It's Best Learned

Trace selection sort, seeing the sorted region grow one element at a time. Implement and count comparisons and swaps. Compare to insertion sort: fewer swaps but same comparison count. Understand why it's not inherently stable.

Common Misconceptions

Explainer

Selection sort works by a simple, almost mechanical process: scan the entire unsorted portion of the array to find the smallest element, then swap it into its correct position at the front. Now that element is sorted. Repeat for the remaining unsorted portion. If you've worked with arrays and indexing, you can already picture this — it's like sorting a hand of playing cards by repeatedly pulling out the lowest card and placing it at the left end of your hand.

Walk through a concrete example. Given the array `[29, 10, 14, 37, 13]`, the algorithm scans all five elements, finds 10 as the minimum, and swaps it with 29 to get `[10, 29, 14, 37, 13]`. Now the sorted region is `[10]` and the unsorted region is `[29, 14, 37, 13]`. Next scan finds 13, swaps with 29: `[10, 13, 14, 37, 29]`. Then 14 is already in place. Then 29 swaps with 37. After n−1 passes, the array is sorted. Notice that regardless of the input, the algorithm always makes the same number of comparisons: n(n−1)/2. Whether the array starts sorted, reversed, or random, selection sort doesn't adapt — it always does O(n²) comparisons.

What selection sort does minimize is swaps — exactly n−1 of them, one per pass. This is the fewest swaps of any comparison-based sorting algorithm, and it matters when writes are expensive (think flash memory where writes degrade the hardware, or scenarios where each swap triggers an expensive side effect). By contrast, insertion sort might make O(n²) swaps on a reversed array but can finish in O(n) on a nearly-sorted one. So the choice between them depends on your data: if it's likely almost sorted, insertion sort adapts and runs fast; if write cost dominates, selection sort's minimal swaps win.

One last detail: selection sort is not stable in its standard form. Stability means equal elements preserve their original relative order. When selection sort swaps the minimum into position, it can jump an element over others with the same value, breaking their original order. For example, if you have two elements with key 5 at positions 2 and 4, a swap might move the one at position 4 ahead of the one at position 2. This usually doesn't matter for simple integers, but it matters when you're sorting records with multiple fields and want to preserve a prior sort's ordering.

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 → Selection Sort Algorithm

Longest path: 83 steps · 398 total prerequisite topics

Prerequisites (2)

Leads To (1)