π 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:
- Look at all the numbers and find the smallest one (thatβs 2).
- Swap it with the first number.
Now the first number is in its correct place. - Move to the next spot, and again find the smallest number in the remaining unsorted part.
- 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
| Case | Time Complexity | Explanation |
|---|---|---|
| Best Case | O(nΒ²) | Still needs to check every pair. |
| Average Case | O(nΒ²) | Works the same way regardless of input order. |
| Worst Case | O(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
| Feature | Description |
|---|---|
| Type | Comparison-based sorting |
| Method | Select the smallest (or largest) element and place it in correct position |
| Time Complexity | O(nΒ²) |
| Space Complexity | O(1) |
| Best For | Small datasets or educational understanding |
| Sorting Style | In-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! β