Skip to content

Commit 69f318d

Browse files
committed
Add selection sort
1 parent 03e8929 commit 69f318d

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

12-selection-sort/selectionSort.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const selectionSort = (arr) => {
2+
for (let i = 0; i < arr.length; i++) {
3+
let lowest = i;
4+
5+
for (let j = i + 1; j < arr.length; j++) {
6+
if (arr[j] < arr[lowest]) {
7+
lowest = j;
8+
}
9+
}
10+
11+
if (i !== lowest) {
12+
[arr[i], arr[lowest]] = [arr[lowest], arr[i]];
13+
}
14+
}
15+
16+
return arr;
17+
};
18+
19+
console.log(selectionSort([1, 35, 2, 23, 3]));

0 commit comments

Comments
 (0)