-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection-sorting.js
More file actions
35 lines (26 loc) · 984 Bytes
/
selection-sorting.js
File metadata and controls
35 lines (26 loc) · 984 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
const input = require("./input");
(async() => {
const numbers = await input("Enter numbers (Separated by input): ");
const num_list = numbers.replace(/\s/g, "").split(",").map(Number);
function selectionSort(arr) {
// Make original copy
const origCopy = [...arr];
const sortedArray = [];
function geMin(arr) {
// Assume the first number is the highest:
let maxNum = arr[0];
arr.forEach(item => {
if (item < maxNum) maxNum = item;
});
return maxNum;
}
while (origCopy.length > 0) {
const smallestNumber = geMin(origCopy);
sortedArray.push(smallestNumber);
const index = origCopy.indexOf(smallestNumber);
if (index > -1) origCopy.splice(index, 1);
}
return sortedArray;
}
console.log(`The new updated orders of number are: ${selectionSort(num_list).join(", ")}`);
})();