Skip to content

Commit 32a6206

Browse files
committed
Selection sort is fixed
1 parent 0521702 commit 32a6206

1 file changed

Lines changed: 19 additions & 16 deletions

File tree

‎src/sorting/selectionsort.js‎

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
var array = [2,3,1,1,2,4,6,7,8,2,3,5,6,8];
2-
1+
/**
2+
* Selection sort. It's complexity is O(n^2)
3+
*
4+
* @public
5+
* @param {array} array Array to be sorted
6+
* @return {array} The sorted array
7+
*/
38
function selectionSort(array) {
4-
var min,
5-
idx,
6-
temp;
9+
var min, idx, temp;
710
for (var i = 0; i < array.length; i += 1) {
8-
min = Infinity;
9-
for (var j = i + 1; j < array.length; j += 1) {
10-
if (min > array[j]) {
11-
min = array[j];
12-
idx = j;
13-
}
14-
}
15-
temp = array[idx];
16-
array[idx] = array[i];
17-
array[i] = temp;
11+
idx = i;
12+
min = array[i];
13+
for (var j = i + 1; j < array.length; j += 1) {
14+
if (min > array[j]) {
15+
min = array[j];
16+
idx = j;
17+
}
18+
}
19+
temp = array[i];
20+
array[i] = min;
21+
array[idx] = temp;
1822
}
1923
return array;
2024
}
2125

22-
console.log(selectionSort(array));

0 commit comments

Comments
 (0)