Skip to content

Commit 5013236

Browse files
committed
sorting algorithm
1 parent 61532f5 commit 5013236

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

selection_sort.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const numbers = [12, 54, 1, 67, -5, 9, 76, 17];
2+
3+
function selectionSort(array) {
4+
for (let i = 0; i < array.length; i++) {
5+
let min = i;
6+
for (let j = i+1; j < array.length; j++) {
7+
if(array[j] < array[min]){
8+
min = j;
9+
}
10+
}
11+
const temp = array[i];
12+
array[i] = array[min];
13+
array[min] = temp;
14+
}
15+
return array;
16+
}
17+
18+
const sorted = selectionSort(numbers);
19+
// console.log(sorted);

0 commit comments

Comments
 (0)