Selection Sort
Sortingeasy#2Find minimum element and place it at the beginning repeatedly
Ready
Step 1 of 0
Speed
Input Data
Comma-separated numbers (0-999). Min 2 values.
Algorithm Code
1function selectionSort(arr) {2 const n = arr.length;3 for (let i = 0; i < n - 1; i++) {4 let minIdx = i;5 for (let j = i + 1; j < n; j++) {6 if (arr[j] < arr[minIdx]) {7 minIdx = j;8 }9 }10 [arr[i], arr[minIdx]] = [arr[minIdx], arr[i]];11 }12 return arr;13}
Best
O(n²)
Average
O(n²)
Worst
O(n²)
Space
O(1)
About Selection Sort
Selection Sort divides the input list into a sorted and an unsorted region. It repeatedly selects the smallest element from the unsorted region and moves it to the end of the sorted region. Simple but inefficient for large datasets.
Time Complexity: Best: O(n²), Average: O(n²), Worst: O(n²). Space: O(1).