Selection Sort is a simple and intuitive sorting algorithm. Imagine organizing a scattered pile of papers with numbers on them in ascending order. Selection Sort mimics this process by repeatedly selecting the smallest number and placing it in the correct position.
for i from 0 to n-2:
minIndex = i
for j from i+1 to n-1:
if arr[j] < arr[minIndex]:
minIndex = j
swap arr[i] with arr[minIndex]
Selection Sort is a straightforward algorithm that performs well on small datasets. Its simplicity makes it a good choice for educational purposes, despite its O(N²) time complexity.
Bubble Sort is another simple sorting algorithm that is easy to understand. It repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.
n = size of array
for i from 0 to n-2:
for j from 0 to n-2-i:
if arr[j] > arr[j+1]:
swap arr[j] with arr[j+1]