55
Selection Sort - Typescript
Selection sort is simple and easy to implement. But it is also very inefficient in terms of time complexity.
In selection sort, we loop through the array by selecting the smallest value and then swapping it to the left side of the array till it is sorted in ascending order.

const selectionSort = (arr: number[]): number[] => {
const len: number = arr.length;
let minInd: number = -1;
for (let i = 0; i < (len - 1); i++) {
minInd = i
for (let j = (i + 1); j < len; j++) {
if (arr[j] < arr[minInd]) {
minInd = j
}
}
if (minInd !== i) {
[arr[i], arr[minInd]] = [arr[minInd], arr[i]];
}
}
return arr;
}
const result = selectionSort([64, 25, 12, 22, 11]);
Output: [11, 12, 22, 25, 64]
Here how the code is working
That’s all for the selection sort, thanks for reading!
55