-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.js
40 lines (33 loc) · 862 Bytes
/
main.js
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
function selection_sort(arr) {
for (let i = 0; i < arr.length; ++i) {
idx = i;
for (let j = i + 1; j < arr.length; ++j) {
if (arr[idx] > arr[j]) {
idx = j;
}
}
if (idx != i) {
[arr[idx], arr[i]] = [arr[i], arr[idx]];
}
}
}
function generateRandomArray(num, rangeFrom, rangeTo) {
if (rangeTo < rangeFrom) {
return;
}
const arr = new Array(num);
const range = rangeTo - rangeFrom + 1;
for (let i = 0; i < num; ++i) {
arr[i] = Math.floor(Math.random() * range + rangeFrom);
}
return arr;
}
const num = 20;
const rangeFrom = 1;
const rangeTo = 10000;
const arr = generateRandomArray(20, 1, 1000)
console.log("Original array:");
console.log(arr);
selection_sort(arr)
console.log("Sorted array:");
console.log(arr);