-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssort.js
405 lines (287 loc) · 7.02 KB
/
ssort.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
const SELECTIONSORT = 0;
const INSERTIONSORT = 1;
const QUICKSORT = 2;
const MERGESORT = 3;
const SET = 0;
const MARK = 1;
const canvasWidth = 300;
const canvasHeight = 150;
const defaultSize = 50;
const unmarkedColor = 'rgb(0, 0, 0)';
const markedColor = 'rgb(200, 0, 0)';
const partitionColor = 'rgb(148,0,211)';
const maxArraySize = 300;
var arr = []; // the current array
var arrsz = 0; // the current size of the array
var temp = []; // helper array for sorting
/*
list of instructions for the sorting
each element has list of elements to set
and elements to mark
*/
var todo = [];
var whichOne = SELECTIONSORT;
var todoInd = 0; // which instruction to do right now
var animStarted = false; // is the automatic animation running?
var todoPrepared = false; // have the instructions been prepared?
// generates a random number
var randHelper = 1;
function grandint() {
randHelper += 1;
randHelper *= 696969420;
randHelper %= 1000000007;
return randHelper;
}
// generates a random array with the current length
function genarray(l) {
var rem = [];
for (var i = 1; i <= l; i++)
rem.push(i);
arr = [];
arrsz = l;
for (var i = l - 1; i >= 0; i--) {
var cind = grandint() % (i + 1);
arr.push(rem[cind]);
rem[cind] = rem[i];
}
}
// draws the current array
function drawArray() {
var canvas = document.getElementById("array");
var ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
ctx.fillStyle = unmarkedColor;
var eachWidth = canvasWidth / arrsz;
var baseHeight = canvasHeight / (arrsz + 1);
for (var i = 0; i < arrsz; i++) {
ctx.fillRect(i * eachWidth, canvasHeight - (baseHeight * arr[i]), eachWidth, arr[i] * baseHeight);
}
}
// marks the element at the given index on the canvas
// if domark is false, unmarks the element
function mark(ind, whatMark = markedColor) {
var canvas = document.getElementById("array");
var ctx = canvas.getContext("2d");
ctx.fillStyle = whatMark;
var eachWidth = canvasWidth / arrsz;
var baseHeight = canvasHeight / (arrsz + 1);
ctx.fillRect(ind * eachWidth, canvasHeight - (baseHeight * arr[ind]), eachWidth, arr[ind] * baseHeight);
}
function prepAll() {
drawArray();
animStarted = false;
temp = [];
todo = [];
todoInd = 0;
todoPrepared = false;
prepareSort();
}
// generates an array with the current length
// resets all the animations
function makeArray() {
if (arrsz == 0)
arrsz = defaultSize;
genarray(arrsz);
prepAll();
}
// makes a reversed array with the current length
function makeReverseArray() {
if (arrsz == 0)
arrsz = defaultSize;
arr = [];
for (var i = 0; i < arrsz; i++)
arr.push(arrsz - i);
drawArray();
prepAll();
}
// sets the algorithm to do
function setAlgo(algoChosen) {
whichOne = algoChosen;
makeArray();
}
// runs selection sort
function selectionSort() {
for (var i = 0; i < arr.length; i++)
temp.push(arr[i]);
for (var i = 0; i < temp.length; i++) {
todo.push([[], [i]]);
var cmin = i;
for (var j = i + 1; j < temp.length; j++) {
todo.push([[], [cmin, j]]);
if (temp[cmin] > temp[j])
cmin = j;
}
todo.push([[ [i, temp[cmin]], [cmin, temp[i]] ], []]);
var cv = temp[cmin];
temp[cmin] = temp[i];
temp[i] = cv;
}
}
function insertionSort() {
for (var i = 0; i < arr.length; i++)
temp.push(arr[i]);
for (var i = 0; i < temp.length; i++) {
todo.push([[], [i]]);
for (var j = i - 1; j >= 0; j--) {
if (temp[j + 1] < temp[j]) {
todo.push([ [[j, temp[j + 1]], [j + 1, temp[j]]],
[j, j + 1] ]);
var cv = temp[j];
temp[j] = temp[j + 1];
temp[j + 1] = cv;
}
else break;
}
}
todo.push([[],[]]);
}
function mergeSortRec(l, r) {
if (l == r) {
todo.push([[], [l]]);
return;
}
var mid = Math.floor((l + r) / 2);
mergeSortRec(l, mid);
mergeSortRec(mid + 1, r);
var csorted = [];
var p1 = l;
var p2 = mid + 1;
while (p1 <= mid || p2 <= r) {
if (p1 > mid) {
todo.push([[], [p2]]);
csorted.push(temp[p2]);
p2 += 1;
}
else if (p2 > r) {
todo.push([[], [p1]]);
csorted.push(temp[p1]);
p1 += 1;
}
else {
if (temp[p1] < temp[p2]) {
todo.push([[], [p1, p2]]);
csorted.push(temp[p1]);
p1 += 1;
}
else {
todo.push([[], [p1, p2]]);
csorted.push(temp[p2]);
p2 += 1;
}
}
}
topush = [];
for (var i = 0; i < csorted.length; i++) {
topush.push([l + i, csorted[i]]);
temp[l + i] = csorted[i];
}
todo.push([topush, []]);
}
function mergeSort() {
for (var i = 0; i < arr.length; i++)
temp.push(arr[i]);
mergeSortRec(0, arr.length - 1);
}
function quickSortRec(l, r) {
if (l > r) return;
if (l == r) {
todo.push([[], [l]]);
return;
}
var cind = l;
var oput = r;
while (cind < oput) {
todo.push([[], [[cind, partitionColor], cind + 1, oput]]);
if (temp[cind] > temp[cind + 1]) {
todo.push([[ [cind, temp[cind + 1]], [cind + 1, temp[cind]] ],
[cind, [cind + 1, partitionColor], oput]]);
var cv = temp[cind + 1];
temp[cind + 1] = temp[cind];
temp[cind] = cv;
cind += 1;
}
else {
todo.push([[ [cind + 1, temp[oput]], [oput, temp[cind + 1]] ],
[[cind, partitionColor], cind + 1, oput]]);
var cv = temp[cind + 1];
temp[cind + 1] = temp[oput];
temp[oput] = cv;
oput -= 1;
}
}
quickSortRec(l, cind - 1);
quickSortRec(cind + 1, r);
}
function quickSort() {
for (var i = 0; i < arr.length; i++)
temp.push(arr[i]);
quickSortRec(0, arr.length - 1);
todo.push([[],[]]);
}
function prepareSort() {
if (todoPrepared) return;
if (whichOne == SELECTIONSORT)
selectionSort();
if (whichOne == INSERTIONSORT)
insertionSort();
if (whichOne == MERGESORT)
mergeSort();
if (whichOne == QUICKSORT)
quickSort();
todoPrepared = true;
}
// takes the next step in sorting
function nextStep() {
if (todoInd == todo.length) {
animStarted = false;
return false;
}
for (var i = 0; i < todo[todoInd][SET].length; i++)
arr[todo[todoInd][SET][i][0]] = todo[todoInd][SET][i][1];
drawArray();
for (var i = 0; i < todo[todoInd][MARK].length; i++) {
if (Array.isArray(todo[todoInd][MARK][i])) {
mark(todo[todoInd][MARK][i][0], todo[todoInd][MARK][i][1]);
}
else
mark(todo[todoInd][MARK][i]);
}
todoInd += 1;
return true;
}
// next step of the animation
function doanim() {
if (animStarted == false) return;
var canvas = document.getElementById("array");
var ctx = canvas.getContext("2d");
var keepdoing = nextStep();
if (keepdoing == true) {
window.requestAnimationFrame(doanim);
}
}
// start the animation
function startAnim() {
if (animStarted == true) return;
animStarted = true;
doanim();
}
// stop the animation
function stopAnim() {
animStarted = false;
}
// generates an array with the user-given size
function regenArray() {
var userChosenSize = document.getElementById("chosenSize").value;
userChosenSize = parseInt(userChosenSize);
var ok = true;
if (typeof(userChosenSize) != "number") ok = false;
if (userChosenSize < 1 || userChosenSize > maxArraySize) ok = false;
if (isNaN(userChosenSize)) ok = false;
if (ok == false) {
arrsz = 0;
}
else {
arrsz = userChosenSize;
}
makeArray();
}