Skip to content

Commit d3e4d23

Browse files
authored
add selection sort (Py-Contributors#906)
1 parent 9136695 commit d3e4d23

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function selectionSort(arr) {
2+
for (let i = 0; i < arr.length; i++) {
3+
let lowest = i
4+
for (let j = i + 1; j < arr.length; j++) {
5+
if (arr[j] < arr[lowest]) {
6+
lowest = j
7+
}
8+
}
9+
if (lowest !== i) {
10+
// Swap
11+
;[arr[i], arr[lowest]] = [arr[lowest], arr[i]]
12+
}
13+
}
14+
return arr
15+
}
16+
console.log(selectionSort([3, 5, 1, 2])) // [1, 2, 3, 5]

0 commit comments

Comments
 (0)