π Selection Sort
Introductionβ
Selection Sort is a simple comparison-based sorting algorithm. It repeatedly selects the smallest (or largest) element from the unsorted part and moves it to the beginning (or end) of the list.
Complexityβ
- Time: O(nΒ²) in the worst and average case
- Space: O(1) (in-place)
How It Worksβ
- Find the minimum element in the unsorted part
- Swap it with the first unsorted element
- Move the boundary of the sorted part one step forward
- Repeat until the list is sorted
Implementationβ
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]
Usage Exampleβ
arr = [64, 25, 12, 22, 11]
selection_sort(arr)
print(arr) # Output: [11, 12, 22, 25, 64]
Advantages and Disadvantagesβ
β Advantagesβ
- Simple to understand and implement
- Does not require extra memory (in-place)
- Performs well on small lists
β Disadvantagesβ
- Very slow for large lists (O(nΒ²))
- Not stable by default (can be made stable with modifications)
When to Use Selection Sortβ
- For educational purposes and learning sorting concepts
- When working with very small datasets
Related Algorithmsβ
- Bubble Sort: Swaps adjacent elements to sort
- Insertion Sort: Builds the sorted array one item at a time