Ready — enter numbers and click Create
Speed
🎨 Customize Colors
Scene Background
Cube Color
Comparing Highlight
Text Color
Sorted Color

📊 Selection Sort

Selection sort works by repeatedly selecting the smallest element from the unsorted portion of the list and moving it to the front. It maintains two sub-portions in the array: the sorted portion at the left and the unsorted portion at the right.

⏱ Best: O(n²) ⏱ Average: O(n²) ⏱ Worst: O(n²) 💾 Space: O(1) ❌ Unstable
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
JavaScript
function selectionSort(arr) {
    const 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;
}