forked from cripi-javascript/dz-1-introduction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysort.js
80 lines (59 loc) · 1.7 KB
/
mysort.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
75
76
77
78
79
80
/*global alert: true*/
/**
* Сортирует массив "пузырьком"
* @param {array} array - массив, который необходимо отсортировать
*/
function sortMassive(array) {
"use strict";
var l = array.length, j, i, temp;
for (j = 0; j < l; j += 1) {
for (i = 0; i < l; i += 1) {
if (array[j] < array[i]) {
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
}
/**
*Сравнивает массивы - отсортированный пузырьком и простой функцией sort
*/
function compareArray(real, expected) {
"use strict";
if (real.length !== expected.length) {
alert("Error not equal length");
return;
}
var i, l = real.length;
for (i = 0; i < l; i += 1) {
if (real[i] !== expected[i]) {
alert("Error!");
return;
}
}
alert("OK");
}
function sortfunction(a, b) {
"use strict";
return a - b;
}
function test1() {
"use strict";
var realArray = [2, 6, 4, 7, 1, 9, 3, 5, 8], expectedArray = [2, 6, 4, 7, 1, 9, 3, 5, 8];
sortMassive(realArray);
expectedArray.sort(sortfunction);
compareArray(realArray, expectedArray);
}
function test2() {
"use strict";
var realArray = [2, 6, 4, 7, 1, 10, 13, 15, 8], expectedArray = [1, 2, 4, 6, 7, 8, 10, 13, 15];
sortMassive(realArray);
expectedArray.sort(sortfunction);
compareArray(realArray, expectedArray);
}
function runTestsSort() {
"use strict";
test1();
test2();
}