Selection Sort
Selection Sort
Selection Sort is a straightforward comparison-based sorting algorithm that divides the array into two parts: sorted and unsorted. It repeatedly selects the smallest (or largest) element from the unsorted portion and moves it to the sorted portion.
How It Worksโ
- Find the minimum element in the unsorted portion
- Swap it with the first unsorted element
- Expand the boundary between sorted and unsorted portions
- Repeat until the entire array is sorted
Step-by-Step Walkthroughโ
Given array: [64, 25, 12, 22, 11]
Pass 1: Find minimum 11, swap with 64 -> [11, 25, 12, 22, 64]
Pass 2: Find minimum 12 (in unsorted), swap with 25 -> [11, 12, 25, 22, 64]
Pass 3: Find minimum 22, swap with 25 -> [11, 12, 22, 25, 64]
Pass 4: Find minimum 25, already in place -> [11, 12, 22, 25, 64]
Implementationโ
Pythonโ
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i + 1, n):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
return arr
# Example usage
arr = [64, 25, 12, 22, 11]
selection_sort(arr)
print(arr) # Output: [11, 12, 22, 25, 64]
JavaScriptโ
function selectionSort(arr) {
let n = arr.length;
for (let i = 0; i < n; i++) {
let minIdx = i;
for (let j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
[arr[i], arr[minIdx]] = [arr[minIdx], arr[i]];
}
return arr;
}
Complexity Analysisโ
| Case | Time Complexity | Space Complexity |
|---|---|---|
| Best Case | ||
| Average Case | ||
| Worst Case |
Selection Sort always performs comparisons, regardless of input order, giving it consistent behavior.
Key Characteristicsโ
Swapsโ
One key advantage of Selection Sort is that it performs at most swaps, making it excellent when write operations are expensive (e.g., flash memory, where each write wears the memory).
Not Adaptiveโ
Unlike Insertion Sort, Selection Sort does not benefit from partially sorted input. The number of comparisons is always .
Not Stableโ
The standard Selection Sort is not stable. When swapping equal elements, their relative order may change.
Stable Variantโ
A stable version can be implemented by shifting elements instead of swapping:
def stable_selection_sort(arr):
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i + 1, n):
if arr[j] < arr[min_idx]:
min_idx = j
# Shift elements right to make room
key = arr[min_idx]
k = min_idx
while k > i:
arr[k] = arr[k - 1]
k -= 1
arr[i] = key
return arr
When to Use Selection Sortโ
- Write-heavy environments: When swapping is expensive and comparisons are cheap
- Educational purposes: Simple to understand and implement
- Memory-constrained systems: space overhead
- Small datasets: Acceptable for datasets up to ~1000 elements
Comparison with Other O(n^2) Algorithmsโ
| Feature | Selection Sort | Insertion Sort | Bubble Sort |
|---|---|---|---|
| Time (best) | |||
| Time (worst) | |||
| Adaptive | No | Yes | Yes |
| Stable | No (can be) | Yes | Yes |
| Swaps (worst) |
Pseudocodeโ
for i = 0 to n-2:
min_idx = i
for j = i+1 to n-1:
if A[j] < A[min_idx]:
min_idx = j
swap A[i] and A[min_idx]
Done with this topic? Mark it as complete to track your progress.