Загрузка...

Selection Sort — The algorithm that treats EVERY swap like it's the last #algorithms

Selection Sort — The algorithm that treats EVERY swap like it's the last | Red-to-green-to-blue O(n²) minimum-finding visualization
See how this methodical algorithm scans the entire unsorted section to find the absolute minimum, then places it at the front with one single permanent swap. Gradient elements flow from red through green to blue as the search pointer sweeps across the array like a laser — scanning every remaining value before committing to one deliberate, irreversible placement. No wasted moves, no temporary reshuffling, no take-backs. One scan, one swap, one element locked into position forever. The sorted section blooms from warm red through verdant green into cool blue as order conquers chaos one precise move at a time.
One of the simplest and most predictable sorting algorithms in existence. Selection Sort always performs exactly O(n²) comparisons regardless of input — best case, worst case, random case, fully sorted case — all identical. Zero adaptiveness. But it compensates with the fewest swaps of any comparison-based algorithm — exactly O(n). This makes it the optimal choice when writes are astronomically more expensive than reads: flash memory with limited program-erase cycles, EEPROMs, SSDs, and any storage medium where every write costs physical lifespan. Not stable in its standard form, but its absolute mechanical predictability makes it a cornerstone of algorithm education alongside Bubble Sort and Insertion Sort.
📝 PSEUDOCODE:

procedure selectionSort(A):
n = length(A)

for i = 0 to n - 2:
minIndex = i

// Find minimum in unsorted section
for j = i + 1 to n - 1:
if A[j] ≺ A[minIndex]:
minIndex = j

// Swap minimum into position
if minIndex != i:
swap(A[i], A[minIndex])
🐍 PYTHON:

def selection_sort(arr):
n = len(arr)

for i in range(n - 1):
min_index = i

# Find minimum in unsorted section
for j in range(i + 1, n):
if arr[j] ≺ arr[min_index]:
min_index = j

# Swap minimum into position
if min_index != i:
arr[i], arr[min_index] = arr[min_index], arr[i]

return arr
🔗 Want to compare all three elementary sorting algorithms head to head? Check out our other algorithm visualizations to explore how Bubble Sort, Insertion Sort, and Selection Sort each tackle the same problem with completely different philosophies. Subscribe and hit the bell to build your algorithm knowledge from the ground up!
🔴🟢🔵 Subscribe for daily algorithm visualizations

Видео Selection Sort — The algorithm that treats EVERY swap like it's the last #algorithms канала Bip Bop Bip Boop Algorithmic Sorting
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять