-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfew_algorithms.html
188 lines (149 loc) · 6.13 KB
/
few_algorithms.html
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Объект Array</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<h1>Алгоритм відсіювання дублюючих елементів в масиві</h1>
<pre>
<script type="text/javascript">
// Создание массива
var firstArray = new Array();
firstArray[0] = "red";
document.writeln("firstArray.length is " + firstArray.length);
var secondArray = new Array("red", "green", "blue");
document.writeln("secondArray.length is " + secondArray.length);
for(var i = 0; i < secondArray.length; i++)
document.writeln(secondArray[i]);
document.writeln(secondArray);
var thirdArray = new Array(5);
document.writeln(thirdArray);
//Алгоритм відсіювання дублюючих елементів в масиві
var algorithmArr = [1,2,3,4,5,1,1],
tempArr = new Array(algorithmArr.length),
counter = 0,
sorting = new Object(),
temporaryVar;
document.writeln();
/*document.writeln("- - -Початок алгоритму - - -");
//виводимо заданий масив
document.writeln("Algorithm Array is " + algorithmArr);
//забиваємо нулями тимчасовий додатковий масив, довжиною з заданий
for (var y = 0; y < tempArr.length; y++)
tempArr[y] = 0;
//Тіло алгоритму
for (var i = 0; i < algorithmArr.length; i++)
for (var j = i + 1; j < algorithmArr.length; j++) {
if (algorithmArr[i] === algorithmArr[j]) {
algorithmArr[j] = null;
}
}
for (var i = 0; i < algorithmArr.length; i++)
if (algorithmArr[i] !== null)
counter++;
document.writeln("Edited array is " + algorithmArr + " " + counter++); //Algorithm Array is 5,4,2,4,5 //Edited array is 5,4,2,0,0
document.writeln(tempArr);
document.writeln("- - - Кінець Алгоритму - - -"); */
document.writeln();
//counting of the repeating elements of the array
//виводимо заданий масив
document.writeln("Algorithm Array is " + algorithmArr);
for (var i = 0; i < algorithmArr.length; i++)
for (var j = i + 1; j < algorithmArr.length; j++) {
if (algorithmArr[i] === algorithmArr[j]) {
if (!sorting[algorithmArr[j]]){
sorting[algorithmArr[j]] = 1;
sorting[algorithmArr[j]]++;
}
else
sorting[algorithmArr[j]]++;
//sorting[algorithmArr[j]] = algorithmArr[j];
/*temporaryVar = algorithmArr[j];
sorting.temporaryVar = temporaryVar;
document.write("temp var is " + sorting.temporaryVar);*/
/*for (var property in sorting)
document.writeln(property + " = " + sorting[property]);*/
/*if (!sorting.algorithmArr[j]){
sorting.algorithmArr[j] = 0;
sorting.algorithmArr[j]++;
}
else
sorting.algorithmArr[j]++;
*/
algorithmArr[j] = null;
}
}
/*for (var i = 0; i < algorithmArr.length; i++)
if (algorithmArr[i] !== null)
counter++;
document.writeln("Edited array is " + algorithmArr + " " + counter++); //Algorithm Array is 5,4,2,4,5 //Edited array is 5,4,2,0,0*/
document.writeln("Fields of the objects are: ")
for (var property in sorting)
document.writeln(property + " = " + sorting[property]);
//sorting.firstElement = 5;
//document.writeln("First element is " + sorting.firstElement);
/*
// Удаление элемента массива
var myColors = new Array("red", "green", "blue");
document.writeln(myColors);
//delete myColors[1];
// myColors[1] = undefined;
//document.writeln(myColors);
var lastElement = myColors.pop();
document.writeln(myColors);
document.writeln(lastElement);
myColors.push("yellow")
document.writeln(myColors);
var firstElement = myColors.shift();
document.writeln(myColors);
document.writeln(firstElement);
myColors.unshift("pink");
document.writeln(myColors);
var splitted = myColors.splice(1,1);
document.writeln(myColors);
document.writeln(splitted[0]);
var s = myColors.join("~");
document.writeln(s);
// Манипуляция с элементами массивов
var str = "Саша,Маша,Петя,Даша";
var users = str.split(",");
for(var i = 0; i < users.length; i++)
document.writeln(users[i]);
document.writeln(users.reverse());
users.sort();
document.writeln(users);
var arr = new Array(40,4,5,31,50,1,10,2,3,30);
arr.sort(compareNumbers);
document.writeln(arr);
/*
document.writeln(users[1]);
document.writeln(users.join("~"));
// Произвольная сортировка массива
// Укажем функцию стравнения
arr.sort(fastCompareNumbers);
document.writeln(arr);
*/
/*
** Функция сравнения чисел
** возвращает: 0 - числа равны
** 1 - левое число больше
** -1 - правое число больше
*/
function compareNumbers(n1, n2)
{
if (n1 == n2) return 0;
if (n1 > n2)
return 1;
else
return -1;
}
// Быстрая функция сравнения - всего одна операция!
function fastCompareNumbers(n1, n2)
{
return n1 - n2;
}
</script>
</pre>
</body>
</html>