Previous Topic
overview_topic
0.78
Establishes the criteria and vocabulary (keys, order, stability, in-place, complexity) used to analyze and understand selection sort.

Selection Sort

algorithms data structures software engineering
Selection sort is a simple comparison-based sorting algorithm that repeatedly selects the smallest remaining element and moves it to its correct position. It runs in quadratic time, uses constant extra space, performs at most n−1 swaps, and is typically not stable without modifications. While inefficient for large datasets, it is easy to understand, implement, and reason about, making it useful for teaching and for small arrays where minimizing swaps matters.

What Is Selection Sort?

Selection sort is a straightforward, comparison-based sorting algorithm. It partitions the array into a sorted prefix and an unsorted suffix. On each pass, it finds the smallest (or largest) element in the unsorted portion and swaps it into the next position of the sorted prefix.

How It Works: Step-by-Step

  1. Start with the first index i = 0.
  2. Scan the unsorted portion (i through end) to find the index of the minimum element.
  3. Swap that minimum element with the element at index i.
  4. Increment i and repeat until the array is sorted.

Pseudocode

procedure selection_sort(A):
  n = length(A)
  for i from 0 to n - 2:
    min_index = i
    for j from i + 1 to n - 1:
      if A[j] < A[min_index]:
        min_index = j
    if min_index != i:
      swap A[i], A[min_index]
  return A

Example Walkthrough

Array: [64, 25, 12, 22, 11]

  1. i=0: Minimum in [64, 25, 12, 22, 11] is 11 → swap with 64 → [11, 25, 12, 22, 64]
  2. i=1: Minimum in [25, 12, 22, 64] is 12 → swap with 25 → [11, 12, 25, 22, 64]
  3. i=2: Minimum in [25, 22, 64] is 22 → swap with 25 → [11, 12, 22, 25, 64]
  4. i=3: Minimum in [25, 64] is 25 → swap with 25 (no change) → [11, 12, 22, 25, 64]

Now sorted.

Correctness Intuition

  • Loop invariant: Before each outer-loop iteration i, the first i elements form the i smallest elements in sorted order.
  • Maintenance: We select the smallest element from the remainder and place it at position i.
  • Termination: When i reaches n−1, all elements are in place.

Complexity and Properties

  • Time complexity (best/average/worst): O(n^2) comparisons; about n(n−1)/2 comparisons in total.
  • Swaps: At most n−1, which is fewer than many simple sorts (e.g., bubble sort).
  • Space complexity: O(1) extra space (in-place).
  • Stability: Not stable by default (equal elements can be reordered). A stable variant can be implemented by inserting the minimum via shifting instead of swapping.
  • Adaptivity: Not adaptive; performance does not improve on partially sorted input.

When to Use

  • Small arrays where implementation simplicity is valued.
  • Situations where minimizing the number of writes/swaps matters (e.g., limited write endurance).
  • Educational contexts to illustrate selection and in-place sorting concepts.

Variations and Related Ideas

  • Stable selection sort: Instead of swapping, remove the minimum and shift elements right to maintain stability (higher constant factors).
  • Bidirectional selection sort: In each pass, select both minimum and maximum and place them at both ends, reducing passes by half but still O(n^2).
  • Heapsort: Can be seen as an optimized selection process using a heap to reduce selection from O(n) to O(log n), achieving O(n log n) time.

Common Pitfalls

  • Incorrectly updating the minimum index during the inner loop.
  • Unnecessary swaps when the minimum is already at position i.
  • Assuming stability when using basic swapping.

Practice Ideas

  • Implement selection sort for ascending and descending order.
  • Write a stable variant using element shifting and compare performance.
  • Count comparisons and swaps to empirically verify complexity.
  • Compare with insertion sort and bubble sort on various input patterns.

Context from Referenced By

Context from Related Topics
Pop Quiz
Topic: selection_sort
Level: 1
True or False:

Selection sort performs at most n-1 swaps.

Topic: selection_sort
Level: 1
Multiple Choice:

Which of the following is a property of basic selection sort?

Topic: selection_sort
Level: 1
Fill in the Blank:

Selection sort partitions the array into a sorted _____ and an unsorted suffix.

Topic: selection_sort
Level: 2
True or False:

Basic selection sort is not stable unless modified to insert the minimum by shifting instead of swapping.

Topic: selection_sort
Level: 2
Multiple Choice:

For an array of n elements, how many comparisons does basic selection sort perform in total?

Topic: selection_sort
Level: 2
Fill in the Blank:

On each pass, selection sort scans the unsorted portion to find the index of the _____ and swaps it into position i.

Topic: selection_sort
Level: 3
True or False:

Selection sort is not adaptive and performs the same number of comparisons even when the input array is already nearly sorted.

Topic: selection_sort
Level: 3
Multiple Choice:

Which loop invariant best explains the correctness of selection sort's outer loop?

Topic: selection_sort
Level: 3
Fill in the Blank:

Selection sort runs in O(1) extra space because it sorts the array _____.

Topic: selection_sort
Level: 4
True or False:

Bidirectional selection sort remains O(n^2) in time even though it selects both the minimum and maximum in each pass.

Topic: selection_sort
Level: 4
Multiple Choice:

In which scenario is selection sort a reasonable choice despite its O(n^2) time complexity?

Topic: selection_sort
Level: 4
Fill in the Blank:

Heapsort can be viewed as an optimized selection process that reduces the per-selection cost from O(n) in selection sort to _____, leading to O(n log n) time overall.

Topic: selection_sort
Level: 5
True or False:

A stable variant of selection sort that inserts the minimum by shifting elements typically incurs higher constant factors than the basic swapping version.

Topic: selection_sort
Level: 5
Multiple Choice:

In the standard selection sort (outer loop i from 0 to n-2; inner loop j from i+1 to n-1), how many comparisons of A[j] < A[min_index] are performed during a given outer iteration i?

Topic: selection_sort
Level: 5
Fill in the Blank:

In the standard selection sort pseudocode, the outer loop runs i from 0 to _____, resulting in exactly n−1 passes.

Next Topic
related_to
0.88

In Place Algorithms
Selection sort exemplifies the in-place property, motivating the broader concept of in-place algorithms and their space–time trade-offs.
implements
0.88

Comparison Sort
Selection sort is an instance of the comparison-based sorting model, ordering elements solely through pairwise comparisons.
related_to
0.86

Unstable Sorting
Selection sort is a canonical example of an unstable sorting algorithm because its swaps can reorder equal elements unless specifically modified to be stable.
related_to
0.78

Insertion Sort
Commonly studied next to contrast characteristics: both are simple in-place O(n^2) sorts, but insertion sort is stable and adaptive, often outperforming selection sort on nearly sorted data.
used_by
0.78

Algorithm Analysis
Selection sort is a canonical case study for evaluating time/space complexity, stability, and swap counts, motivating Big-O and trade-off discussions.
related_to
0.78

Space Time Tradeoffs
Selection sort highlights the tradeoff between higher time complexity and constant extra space with few swaps, leading into analysis of algorithmic space-time tradeoffs.