-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathselectionsort.js
More file actions
33 lines (29 loc) · 813 Bytes
/
selectionsort.js
File metadata and controls
33 lines (29 loc) · 813 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
const selectionModule = () => {
// swap method because its used multiple times
const swap = (array, index1, index2) => {
// store a tmp variable at pos index2
const tmp = array[index2];
// set value of index2 to our value at index
array[index2] = array[index1];
// set our value of index1 to our stored variable
array[index1] = tmp;
};
// Everything after the return statement is public
return {
selectionSort: array => {
for (let i = 0; i < array.length - 1; i++) {
let min = i;
for (let j = i + 1; j < array.length; j++) {
if (array[j] < array[min]) {
min = j;
}
}
if (min !== i) {
swap(array, i, min);
}
}
return array;
},
};
};
export default selectionModule;