function selectionSort(arr) {
const a = [...arr];
for (let i = 0; i < a.length; i++) {
let min = i;
for (let j = i+1; j < a.length; j++) if (a[j] < a[min]) min = j;
[a[i], a[min]] = [a[min], a[i]];
}
return a;
}Selection sort finds the minimum of the unsorted portion and moves it to the front.
Selection Sort divides the array into two parts: sorted and unsorted. It repeatedly finds the minimum element from the unsorted part and moves it to the end of the sorted part.
Time Complexity: O(nΒ²) Space Complexity: O(1)
function selectionSort(arr: number[]): number[] {
// Implement selection sort
// Find minimum and swap it to correct position
}
selectionSort([64, 25, 12, 22, 11]) // [11, 12, 22, 25, 64]