-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSort.mjs
75 lines (69 loc) · 2 KB
/
Sort.mjs
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//---------------------------------------------//
// Copyright (c) 2020 Ernesto Leonel Argüello.
// This file is covered by LICENSE at the root of this project.
//---------------------------------------------//
export function SortInts(ArrayInInput, Ascending) {
let i = 0;
let j = 0;
let FirstTime = true;
let ArrayIn = ArrayInInput; // Input.
let CurrentChecked = 0; // Number currently checked.
let CurrentCheckedIndex = 0; // Index of the currently checked number.
let UsedIndexes = []; // Shows indexes already used, and in which order.
let ArrayOut = []; // Finished product.
let Compare = { // Cool dynamic operator.
false: function(a, b) { return a > b },
true: function(a, b) { return a < b },
};
function SortIntsLoop1() {
FirstTime = true;
if (ArrayIn.length == UsedIndexes.length) {
return;
}
else {
SortIntsLoop2();
return;
}
}
function SortIntsLoop2() {
if (ArrayIn.length == i) {
ArrayOut.push(CurrentChecked);
UsedIndexes.push(CurrentCheckedIndex);
i = 0;
SortIntsLoop1();
return;
}
else {
SortIntsLoop3();
return;
}
}
function SortIntsLoop3() {
if (UsedIndexes.length == j) {
j = 0;
if (FirstTime || Compare[Ascending](ArrayIn[i], CurrentChecked)) {
CurrentChecked = ArrayIn[i];
CurrentCheckedIndex = i;
FirstTime = false;
}
i++;
SortIntsLoop2();
return;
}
else {
if (UsedIndexes[j] == i) {
j = 0;
i++;
SortIntsLoop2();
return;
}
else {
j++;
SortIntsLoop3();
return;
}
}
}
SortIntsLoop1();
return [ArrayOut, UsedIndexes]; // Out.
}