-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionsort.js
More file actions
45 lines (28 loc) · 811 Bytes
/
selectionsort.js
File metadata and controls
45 lines (28 loc) · 811 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var swap = function(array, firstIndex, secondIndex) {
var temp = array[firstIndex];
array[firstIndex] = array[secondIndex];
array[secondIndex] = temp;
};
var indexOfMinimum = function(array, startIndex) {
var minValue = array[startIndex];
var minIndex = startIndex;
for(var i = minIndex + 1; i < array.length; i += 1) {
if(array[i] < minValue) {
minIndex = i;
minValue = array[i];
}
}
return minIndex;
};
var selectionSort = function(array) {
var min;
for(var j = 0 ; j < array.length; j += 1) {
min = indexOfMinimum(array, j);
swap(array, j, min);
}
return array;
};
var arr = [6, 4, 3, 1, 5];
result = selectionSort(arr);
console.log('result', result);
module.exports = selectionSort;