Skip to main content

Selection Sort

tmdeveloper007
EditReport

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โ€‹

  1. Find the minimum element in the unsorted portion
  2. Swap it with the first unsorted element
  3. Expand the boundary between sorted and unsorted portions
  4. 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โ€‹

CaseTime ComplexitySpace Complexity
Best CaseO(n2)O(n^2)O(1)O(1)
Average CaseO(n2)O(n^2)O(1)O(1)
Worst CaseO(n2)O(n^2)O(1)O(1)

Selection Sort always performs n(nโˆ’1)/2n(n-1)/2 comparisons, regardless of input order, giving it consistent O(n2)O(n^2) behavior.

Key Characteristicsโ€‹

Swapsโ€‹

One key advantage of Selection Sort is that it performs at most nโˆ’1n-1 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 n(nโˆ’1)/2n(n-1)/2.

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: O(1)O(1) space overhead
  • Small datasets: Acceptable for datasets up to ~1000 elements

Comparison with Other O(n^2) Algorithmsโ€‹

FeatureSelection SortInsertion SortBubble Sort
Time (best)O(n2)O(n^2)O(n)O(n)O(n)O(n)
Time (worst)O(n2)O(n^2)O(n2)O(n^2)O(n2)O(n^2)
AdaptiveNoYesYes
StableNo (can be)YesYes
Swaps (worst)O(n)O(n)O(n2)O(n^2)O(n2)O(n^2)

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]
Track Your Progress

Done with this topic? Mark it as complete to track your progress.