-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (64 loc) · 1.74 KB
/
index.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
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
console.clear();
console.log('begin');
let array0 = [];
let compearArray = [];
let compearArray1 = [];
let win = 'compairson passed';
let loose = 'U loose';
randomArray(array0);
compearArray = compearArray1 = array0;
console.log(array0);
bobbleSortFor(array0);
console.log('sorted by FOR');
console.log(array0);
bobbleSortWhile(compearArray, 0, 0);
console.log('sorted by WHILE');
console.log(compearArray);
array0 === compearArray ? console.log(win) : console.log(loose);
console.log();
bobbleSortDoWhile(compearArray1);
console.log('sorted by DO WHILE');
console.log(compearArray1);
array0 === compearArray1 ? console.log(win) : console.log(loose);
function randomArray(array, length = 11) {
for (i = 0; i < length; i++) {
array.push(Math.round(Math.random() * 2000) - 1000)
}
}
function bob(array) {
array.sort(function(a, b) {
return a - b;
})
return array
}
function bobbleSortFor(array) {
for (let n = 0; n < array.length; n++) {
for (let i = 0; i < array.length - 1 - n; i++) {
if (array[i] > array[i + 1]) {
[array[i], array[i + 1]] = [array[i + 1], array[i]]
}
}
}
}
function bobbleSortWhile(array, n = 0, i = 0) {
while (n < array.length) {
while (i < array.length - n) {
if (array[i] > array[i + 1]) {
[array[i], array[i + 1]] = [array[i + 1], array[i]]
}
++i;
}
++n;
}
}
function bobbleSortDoWhile(array, i = 0, n = 0) {
do {
do {
if (array[i] > array[i + 1]) {
[array[i], array[i + 1]] = [array[i + 1], array[i]]
}
i++
} while (i < array.length - 1 - n)
n++;
} while (n < array.length - 1)
}