Skip to content

Commit 0cebdda

Browse files
committed
feat: add selection sort
1 parent 9db8197 commit 0cebdda

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

selection_sort/selection_sort.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function findMinimumIndex(arr) {
2+
return arr.reduce((minIndex, current, index) => current < arr[minIndex] ? index : minIndex, 0);
3+
}
4+
5+
function selectionSort(arr) {
6+
const sortedArray = [];
7+
const length = arr.length;
8+
for (let i = 0; i < length; i++) {
9+
let minimumIndex = findMinimumIndex(arr);
10+
sortedArray.push(arr[minimumIndex]);
11+
arr.splice(minimumIndex, 1);
12+
}
13+
return sortedArray;
14+
}
15+
16+
const data = [32, 12, 5, 62, 8, 16, 14];
17+
console.log(selectionSort(data));

0 commit comments

Comments
 (0)