Skip to content

Commit 0480c4d

Browse files
committed
selectionSort/SarahSakhri
1 parent adaa123 commit 0480c4d

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

‎SortingAlgorithms/selectionSort.js‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//Language: Javascript
2+
//Author: Sarah Sakhri
3+
//Github: https://github.com/sarahsakhri
4+
5+
let x = [66, 0, 19, -45, 26, 56, 15, 21, -98];
6+
console.log("---- Original array -----");
7+
console.log(x);
8+
9+
10+
function selectionSort(arr) {
11+
for (i = 0; i < arr.length; i++) {
12+
let min = i;
13+
for (let j = i + 1; j < arr.length; j++) {
14+
if (arr[j] < arr[min]) {
15+
min = j;
16+
}
17+
}
18+
19+
if (i !== min) {
20+
let temp = arr[i];
21+
arr[i] = arr[min];
22+
arr[min] = temp;
23+
}
24+
}
25+
26+
return arr;
27+
}
28+
29+
selectionSort(x);
30+
31+
console.log("---- Selection sorted array -----");
32+
console.log(x);
33+

0 commit comments

Comments
 (0)