Skip to content

Commit 397d5b1

Browse files
authored
Merge pull request argonautica#38 from thakurutkarsh22/master
AddingSelectionSortInJavaScript
2 parents fe86d67 + 190b17b commit 397d5b1

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Javascript/selectionSort.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function selectionSort(A) {
2+
var len = array_length(A);
3+
for (var i = 0; i < len - 1; i = i + 1) {
4+
var j_min = i;
5+
for (var j = i + 1; j < len; j = j + 1) {
6+
if (A[j] < A[j_min]) {
7+
j_min = j;
8+
} else {}
9+
}
10+
if (j_min !== i) {
11+
swap(A, i, j_min);
12+
} else {}
13+
}
14+
}
15+
16+
function swap(A, x, y) {
17+
var temp = A[x];
18+
A[x] = A[y];
19+
A[y] = temp;
20+
}

0 commit comments

Comments
 (0)