-
Notifications
You must be signed in to change notification settings - Fork 0
/
Script.js
269 lines (238 loc) · 8.06 KB
/
Script.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
function Sort() { }; // Empty function from which every function in linked
Sort.container = "#container"; // container id is set
//values of array
Sort.num_boxes = 39; // length of array
Sort.boxes = [];// array of boxes
//variable decleration
Sort.intervalID = 0;
Sort.i = 0;
Sort.j = 0;
Sort.min = 0;
Sort.key = 0;
Sort.mid = 0;
function Box(x, height, lightness) {
this.x = x;
this.height = height;
this.color = "hsl(238, 11%, " + lightness + "19%)";
//A $ sign to define/access jQuery
//A (selector) to "query (or find)" HTML elements
this.div = $("<div>")
//A jQuery action() to be performed on the element(s)
//The addClass() method adds one or more class names to the selected elements.
.addClass("box")
.css({
"height": height,
"left": x
});
this.setBG(true);
};
// It is simply an object from which other objects can inherit properties.
Box.prototype.setBG = function () {
$(this.div).css({
"background": "-webkit-gradient(linear, left top, left bottom, from(" + this.color + "), to(black))"
})
}
Box.prototype.draw = function () {
//The append() method inserts specified content at the end of the selected elements.
$(Sort.container).append(this.div)
}
//.add update the furthur properties of object
Box.prototype.add = function (my) {
this.x = my;
$(this.div).animate({ "left": my }, 20);
}
//swap function
Sort.swap = function (i, j) {
//Value of x is set and swapped with i and j index
var temp = this.boxes[i];
var x = temp.x
this.boxes[i] = this.boxes[j];
this.boxes[j] = temp;
this.boxes[j].add(this.boxes[i].x);
this.boxes[i].add(x);
}
//mix elements or values function
Sort.mix = function () {
for (var i = 0; i < (this.num_boxes * 4); i++) {
//Math.floor round a number downward to its nearest integer:
//Math.random return a random number between 0 (inclusive) and 1 (exclusive):
index_i = Math.floor(Math.random() * this.num_boxes);
index_j = Math.floor(Math.random() * this.num_boxes);
this.swap(index_i, index_j);
}
}
//bubble sort function definition
Sort.bubbleR = function () {
//first and second box is set
first_box = Sort.boxes[Sort.j];
second_box = Sort.boxes[Sort.j + 1];
//Value are compared
if (second_box.height < first_box.height) {
Sort.swap(Sort.j, Sort.j + 1);
}
Sort.j++;
if (Sort.j == Sort.num_boxes - Sort.i - 1) {
Sort.i++;
Sort.j = 0;
if (Sort.i == Sort.num_boxes - 1) {
//clearinternval to stop or speed down the values
clearInterval(Sort.intervalID);
Sort.j = 0;
Sort.i = 0;
}
}
}
//selectionsort function definition
Sort.selectR = function () {
//First box and second box is set
first_box = Sort.boxes[Sort.min];
second_box = Sort.boxes[Sort.j + 1];
//Second box is compared with min
if (second_box.height < first_box.height) {
Sort.min = Sort.j + 1;
}
Sort.j++;
//after one full iteration min is swapped and value of i is increased
//new values of min and j are also set
if (Sort.j == Sort.num_boxes - 1) {
Sort.swap(Sort.min, Sort.i);
Sort.i++;
Sort.min = Sort.i;
Sort.j = Sort.i;
if (Sort.i == Sort.num_boxes - 1) {
//after the value are sorted interval is cleared
clearInterval(Sort.intervalID);
Sort.j = 0;
Sort.i = 0;
Sort.min = 0;
}
}
}
//insertion sort function definition
Sort.insertionR = function () {
Sort.i++;
Sort.key = Sort.i;
Sort.j = Sort.i - 1;
if (Sort.j >= 0 && Sort.boxes[Sort.j].height > Sort.boxes[Sort.key].height) {
Sort.swap(Sort.j, Sort.key);
Sort.key = Sort.j;
Sort.j--;
}
if (Sort.i == Sort.num_boxes - 1) {
clearInterval(Sort.intervalID);
Sort.j = 0;
Sort.i = 0;
}
}
//function calls
Sort.selection = function () {
//setInterval to set the speed
Sort.intervalID = setInterval(Sort.selectR, 40);
}
Sort.insertion = function () {
Sort.intervalID = setInterval(Sort.insertionR, 40);
}
Sort.bubble = function () {
Sort.intervalID = setInterval(Sort.bubbleR, 80);
}
Sort.quicksort = function () {
Sort.intervalID = setInterval(Sort.quicksort, 40);
}
Sort.merge = function (start, mid, end) {
/* var start2 = mid + 1;
if (Sort.boxes[mid].height <= Sort.boxes[start2].height)
return;
while (start <= mid && start2 <= end) {
if (Sort.boxes[start].height <= Sort.boxes[start2].height)
++start;
else {
//var value = start2;
var value = Sort.boxes[start2];
var index = start2;
while (index != start) {
//Sort.swap(index, index - 1);
Sort.boxes[index] = Sort.boxes[index - 1];
Sort.boxes[index].add(Sort.boxes[index - 1].x);
Sort.boxes[index].draw();
--index;
}
//Sort.swap(start, value);
Sort.boxes[start] = value;
Sort.boxes[start].add(value.x);
Sort.boxes[start].draw();
start++;
mid++;
start2++;
}
} */
if (!(0 <= start && start <= mid && mid <= end && end <= Sort.num_boxes))
return;
if (start == mid || mid == end)
return;
var left = mid - 1;
var right = mid;
while (start <= left && right < end && Sort.boxes[left].height > Sort.boxes[right].height) {
left--;
right++;
}
var n = right - mid;
for (var i = 0; i < n; i++) {
var j = mid - n + i;
var k = mid + i;
Sort.swap(j, k);
}
Sort.merge(start, left + 1, mid);
Sort.merge(mid, right, end);
//setTimeout(, 50);
//ssetTimeout(, 50);
}
Sort.mergesort = function (l, r) {
Sort.mid = Math.floor(l + (r - l) / 2);
Sort.merge(l, Sort.mid, r);
//setTimeout(, 50);
}
//partition function definition for quick sort
Sort.partition = function (left, right) {
pivot_value = Sort.boxes[left].height;
Sort.swap(left, right - 1) //move pivot to end
store_index = left;
for (var i = left; i < right; i++) {
if (Sort.boxes[i].height < pivot_value) {
Sort.swap(i, store_index);
store_index += 1;
}
}
Sort.swap(store_index, right - 1);
return store_index;
}
//quick sort function definition
Sort.quicksort = function (left, right) {
if (right - left <= 1) {
return
}
var part_i = Sort.partition(left, right)
//settime to set the time or speed but it does not work
setTimeout(Sort.quicksort(left, part_i), 5000000);
setTimeout(Sort.quicksort(part_i + 1, right), 500000);
}
$(document).ready(function () {
//When webpage is opened css of the boxes is set
$(Sort.container).css({
//Container height and width is set
width: (Sort.num_boxes * 25) + 8,
height: (Sort.num_boxes * 10) + 10,
})
//Boxes are generated inside the container
for (var i = 1; i <= Sort.num_boxes; i++) {
lightness = (100 - (2.5 * i)) / 100 * 100 // Creates a random numeric value based on number i
Sort.boxes[i - 1] = new Box((25 * (i - 1)) + 5, 10 * i, lightness); // New box is set at the respective index
Sort.boxes[i - 1].draw();//draw function is called
}
//Function are called when a button is clicked of the respective id
$("#mix").click(function () { Sort.mix() });
$("#bubble").click(function () { Sort.bubble() });
$("#selection").click(function () { Sort.selection() });
$("#insertion").click(function () { Sort.insertion() });
$("#quick").click(function () { Sort.quicksort(0, Sort.boxes.length) });
$("#merge").click(function () { Sort.mergesort(0, Sort.num_boxes - 1) });
})