Skip to content
This repository was archived by the owner on Jun 26, 2022. It is now read-only.

Commit 28cc8d6

Browse files
Merge pull request #144 from vedanttttt/master
Create selectionSort.js
2 parents b6365b9 + 19ba016 commit 28cc8d6

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

js/vedanttttt_selectionSort.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function selectionSort(inputArr) {
2+
let n = inputArr.length;
3+
4+
for(let i = 0; i < n; i++) {
5+
// Finding the smallest number in the subarray
6+
let min = i;
7+
for(let j = 0; j < n; j++){
8+
if(inputArr[j] < inputArr[min]) {
9+
min=j;
10+
}
11+
}
12+
if (min != i) {
13+
// Swapping the elements
14+
let tmp = inputArr[i];
15+
inputArr[i] = inputArr[min];
16+
inputArr[min] = tmp;
17+
}
18+
}
19+
return inputArr;
20+
}

0 commit comments

Comments
 (0)