Selection Sort — Sorting

🌟 What Is Selection Sort?

Selection Sort is a straightforward sorting algorithm that works by repeatedly finding the smallest element from the unsorted part of the list and moving it to the sorted part.

It’s called “Selection” because, at each step, you select the smallest (or largest) element and put it where it belongs.


🧩 The Basic Idea

Imagine you have a bunch of numbers written on sticky notes stuck to your desk:

[7, 4, 9, 2, 5]

You want them arranged from smallest to largest.
Here’s what you do:

  1. Look at all the numbers and find the smallest one (that’s 2).
  2. Swap it with the first number.
    Now the first number is in its correct place.
  3. Move to the next spot, and again find the smallest number in the remaining unsorted part.
  4. Keep repeating until everything is sorted.

That’s it! Simple, right?


🪜 Step-by-Step Example

Let’s sort this list:

[7, 4, 9, 2, 5]

Step 1:

Find the smallest number.
👉 The smallest is 2.
Swap it with the first element (7).

[2, 4, 9, 7, 5]

Now 2 is sorted — it stays there forever. ✔️


Step 2:

Look at the remaining part: [4, 9, 7, 5].
The smallest number is 4.
Swap it with itself (since it’s already in the correct position).

[2, 4, 9, 7, 5]

Step 3:

Next part: [9, 7, 5].
The smallest is 5.
Swap 9 and 5.

[2, 4, 5, 7, 9]

Step 4:

Now only [7, 9] remains.
The smallest is 7, which is already in place.

[2, 4, 5, 7, 9]

🎉 All sorted!


🖼️ Diagram: Visualizing Selection Sort

Initial: [7, 4, 9, 2, 5]

Step 1 → Find min (2)
          [2, 4, 9, 7, 5]

Step 2 → Find min (4)
          [2, 4, 9, 7, 5]

Step 3 → Find min (5)
          [2, 4, 5, 7, 9]

Step 4 → Find min (7)
          [2, 4, 5, 7, 9]

Final Sorted List → [2, 4, 5, 7, 9]

At every step, the left side becomes the sorted zone,
and the right side is still unsorted until it’s all done.


🧠 Simple Pseudocode

Here’s what Selection Sort does, in plain terms:

SelectionSort(arr, n):
    for i from 0 to n-1:
        minIndex = i
        for j from i+1 to n:
            if arr[j] < arr[minIndex]:
                minIndex = j
        swap(arr[i], arr[minIndex])
  • The outer loop keeps track of the sorted part.
  • The inner loop finds the smallest element in the unsorted part.

⚙️ Time Complexity

CaseTime ComplexityExplanation
Best CaseO(n²)Still needs to check every pair.
Average CaseO(n²)Works the same way regardless of input order.
Worst CaseO(n²)No shortcuts, it always does the same amount of work.

Space Complexity: O(1) (because it sorts in-place — no extra memory needed).


🌼 Advantages

✅ Easy to understand and implement.
✅ Doesn’t need extra space — just swaps within the same list.
✅ Works fine for small datasets.


⚠️ Disadvantages

❌ Very slow for large datasets because it always scans the full list for each element.
❌ Not efficient compared to more advanced algorithms like Quick Sort or Merge Sort.


🎯 Real-Life Analogy

Imagine you’re ranking your test scores from lowest to highest.
You look at all your marks, pick the smallest, and write it first on a new sheet.
Then, from the remaining ones, pick the next smallest, and write that next — and so on.

That’s exactly what Selection Sort does — it selects the smallest again and again until everything is in order. ✏️


🧭 In a Nutshell

FeatureDescription
TypeComparison-based sorting
MethodSelect the smallest (or largest) element and place it in correct position
Time ComplexityO(n²)
Space ComplexityO(1)
Best ForSmall datasets or educational understanding
Sorting StyleIn-place and non-stable

🎨 Easy Visual Summary

[7, 4, 9, 2, 5]
↓
Find smallest → 2
[2, 4, 9, 7, 5]
↓
Find next smallest → 4
[2, 4, 9, 7, 5]
↓
Find next smallest → 5
[2, 4, 5, 7, 9]
↓
Sorted! ✅