Intermediate 25 min

What is Selection Sort?

Selection Sort is a simple sorting algorithm that works by repeatedly finding the minimum element from the unsorted portion and placing it at the beginning. The algorithm maintains two subarrays: one sorted and one unsorted.

The Basic Idea

Here’s how selection sort works:

More elements All sorted Start Find Minimum Swap Repeat Sorted!

How It Works

  1. Find minimum: Scan unsorted portion to find minimum element
  2. Swap: Swap minimum with first element of unsorted portion
  3. Move boundary: Move boundary between sorted and unsorted
  4. Repeat: Continue until entire array is sorted

Key Characteristics

  • Time complexity: O(n²) in all cases
  • Space complexity: O(1) - in-place sorting
  • Stable: No - may change relative order of equal elements
  • Simple: Easy to understand and implement

Real-World Analogy

Imagine you have a deck of cards. You find the smallest card and put it at the front. Then you find the smallest from the remaining cards and put it second. Keep doing this until all cards are sorted.

When to Use

Selection sort is rarely used in production because:

  • It’s slow (O(n²))
  • Better algorithms exist
  • It’s not stable

But it’s useful for:

  • Educational purposes
  • Small datasets
  • When memory is very limited
  • Understanding sorting fundamentals

What’s Next?

Now that you know what selection sort is, let’s see exactly how it works step-by-step.

Progress 14%
Page 1 of 7
Previous Next