-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting.js
344 lines (290 loc) · 10.4 KB
/
sorting.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
//GLOBAL VARIABLES
let arrayToSort = [];
let w = 10;
let states = [];
let playbackSpeed = 0;
let algorithmRunning = false;
let canvas = null;
let swaps = 0;
let timeToExecute = 0;
let totalDelay = 0;
//P5.js FUNCTIONS
/*
FUNCTION: SETUP
PARAMETERS: NONE
DESCRIPTION: SETS UP THE CANVAS, CREATES AN ARRAY FULL OF RANDOM NUMBERS WITH IT'S LENGTH
BASED ON SCREEN WIDTH, AND SETS THE STATE OF EACH ITEM IN THE ARRAY.
*/
function setup(){
canvas = createCanvas(windowWidth - 200, windowHeight - 200);
canvas.parent("p5-canvas");
arrayToSort = new Array(floor(width/w));
for(let i = 0; i < arrayToSort.length; i++){
arrayToSort[i] = random(height)
states[i] = -1;
}
}
/*
FUNCTION: DRAW
PARAMETERS: NONE
DESCRIPTION: DRAWS THE GRAPH
*/
function draw(){
background(0, 0, 0);
for(let i = 0; i < arrayToSort.length; i++){
noStroke();
fill(255);
if(states[i] == 0){
fill(255, 0, 0);
} else if (states[i] == 1){
fill(0, 255, 0)
} else{
fill(255)
}
rect(i * w, height - arrayToSort[i], w, arrayToSort[i]);
}
textSize(15);
stroke(0, 0, 0)
strokeWeight(2)
textStyle(BOLD);
text("Array Length: " + arrayToSort.length + "\nSwaps: " + swaps + "\nExecution Time: " + timeToExecute + "ms" + "\nTotal Delay: " + totalDelay + "ms", 20, 20);
}
//SORTING FUNCTIONS
/*
FUNCTION: SWAP
PARAMETERS: arr: The array to swap, a: The first value for the swap,
b: The second value for the swap.
DESCRIPTION: SETS UP THE CANVAS, CREATES AN ARRAY FULL OF RANDOM NUMBERS WITH IT'S LENGTH
BASED ON SCREEN WIDTH, AND SETS THE STATE OF EACH ITEM IN THE ARRAY.
*/
async function swap(arr, a, b){
await sleep(playbackSpeed)
let temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
swaps++;
}
/*
FUNCTION: BUBBLESORT
PARAMETERS: arrayToSort: The array we wish to sort.
DESCRIPTION: PERFORMS THE BUBBLESORT ALGORITHM TO SORT THE ARRAY IN ASCENDING ORDER.
*/
async function bubbleSort(arrayToSort){
for (let i = 0; i < (arrayToSort.length - 1); i++){
for(let j=0; j < (arrayToSort.length - i - 1); j++){
states[j] = 0;
states[j+1] = 1
if(arrayToSort[j] > arrayToSort[j+1]){
await swap(arrayToSort, j, j+1);
}
states[j] = -1;
states[j+1] = -1
}
}
}
/*
FUNCTION: QUICKSORT
PARAMETERS: arr: The array to swap, start: The start of the partition,
end: The end of the partition
DESCRIPTION: PERFORMS THE QUICKSORT ALGORITHM TO SORT IN ASCENDING
ORDER USING LOMUTO PARTITIONING
*/
async function quickSort(arr, start, end){
if(start >= end){
return;
}
let index = await partition(arr, start, end);
states[index] = -1;
await Promise.all([quickSort(arr, start, index-1), quickSort(arr, index+1, end)]);
/*
FUNCTION: PARTITION
PARAMETERS: arr: The array to swap, start: The start of the partition,
end: The end of the partition
DESCRIPTION: PARTITION FUNCTION USING LOMUTO PARTIONING TO SORT EACH PARTITION OF THE ARRAY
*/
async function partition(arr, start, end){
for(let i = start; i < end; i++){
states[i] = 1;
}
let pivotValue = arr[end];
let pivotIndex = start;
states[pivotIndex] = 0;
for(let i = start; i < end; i++){
if(arr[i] < pivotValue){
await swap(arr, i, pivotIndex);
states[pivotIndex] = -1;
pivotIndex++;
states[pivotIndex] = 0;
}
}
await swap(arr, pivotIndex, end);
for(let i = start; i < end; i++){
if(i != pivotIndex)
states[i] = -1;
}
return pivotIndex;
}
}
/*
FUNCTION: INSERTIONSORT
PARAMETERS: arr: The array to sort.
DESCRIPTION: PERFORMS THE INSERTION SORT ALGORITHM TO SORT THE ARRAY IN ASCENDING ORDER
*/
async function insertionSort(arr){
for(let i = 1; i < arr.length; i++){
let current = arr[i];
let j = i-1;
while((j > -1) && (current < arr[j])){
states[j] = 0
states[j-1] = 1
await sleep(playbackSpeed);
arr[j+1] = arr[j];
states[j] = -1
states[j-1] = -1
j--;
swaps++;
}
arr[j+1] = current
}
}
/*
FUNCTION: SELECTIONSORT
PARAMETERS: arr: The array to sort.
DESCRIPTION: PERFORMS THE SELECTION SORT ALGORITHM TO SORT THE ARRAY IN ASCENDING ORDER
*/
async function selectionSort(arr){
for(let i=0; i < arr.length; i++){
let min = i;
for(let j = i + 1; j < arr.length; j++){
if(arr[min] > arr[j]){
min = j;
}
}
states[i] = 1;
states[min] = 0;
if(min !== i){
await swap(arr, i, min);
}
states[min] = -1;
states[i] = -1;
}
}
/*
FUNCTION: MERGESORT
PARAMETERS: arr: The array to sort, leftIndex: the leftmost item in the array, rightIndex: the rightmost item in the array.
DESCRIPTION: PERFORMS THE MERGE SORT ALGORITHM TO SORT THE ARRAY IN ASCENDING ORDER
*/
async function mergeSort(arr, leftIndex, rightIndex){
length = rightIndex - leftIndex
if (length < 2) {
return arr;
}
var mid = leftIndex + Math.floor(length / 2);
await mergeSort(arr, leftIndex, mid);
await mergeSort(arr, mid, rightIndex);
await merge(arr, leftIndex, mid, rightIndex);
/*
FUNCTION: MERGES
PARAMETERS: arr: The array to sort, leftIndex: the leftmost item in the array,
mid: the middle item in the array, rightIndex: the rightmost item in the array.
DESCRIPTION: PERFORMS THE MERGE SORT ALGORITHM TO SORT THE ARRAY IN ASCENDING ORDER
*/
async function merge(arr, leftIndex, mid, rightIndex) {
var result = [];
var l = leftIndex, r = mid;
while (l < mid && r < rightIndex) {
if (arr[l] < arr[r]) {
states[l] = 0;
await sleep(playbackSpeed);
states[l] = -1;
result.push(arr[l++]);
} else {
states[r] = 0;
await sleep(playbackSpeed);
states[r] = -1;
result.push(arr[r++]);
}
swaps++;
}
result = result.concat(arr.slice(l, mid)).concat(arr.slice(r, rightIndex));
for (let i = 0; i < rightIndex - leftIndex; i++) {
states[leftIndex + i] = 1;
await sleep(playbackSpeed);
arr[leftIndex + i] = result[i]
states[leftIndex + i] = -1;
}
}
}
//HELPER FUNCTIONS
/*
FUNCTION: RUN
PARAMETERS: NONE
DESCRIPTION: GETS THE SELECTED ALGORITHM AND SPEED, AND RUNS
THE SELECTED ALGORITHM AT THAT SPEED.
*/
async function run(){
let algorithmStartTime = 0;
let algorithmEndTime = 0;
if(algorithmRunning == false){
swaps = 0;
totalDelay = 0;
var algorithmSelectBox = document.getElementById("algorithm-selection");
var selectedAlgorithm = parseInt(algorithmSelectBox.options[algorithmSelectBox.selectedIndex].value);
var speedSelectBox = document.getElementById("speed-selection");
var selectedSpeed = parseInt(speedSelectBox.options[speedSelectBox.selectedIndex].value);
switch(selectedSpeed){
case 0: playbackSpeed = 0; break;
case 1: playbackSpeed = 1; break;
case 2: playbackSpeed = 5; break;
case 3: playbackSpeed = 25; break;
case 4: playbackSpeed = 50; break;
case 5: playbackSpeed = 100; break;
}
switch(selectedAlgorithm){
case 0: algorithmStartTime = performance.now(); algorithmRunning = true; setSelectStatus(0); await bubbleSort(arrayToSort); setSelectStatus(1); algorithmRunning = false; algorithmEndTime = performance.now(); timeToExecute = (algorithmEndTime-algorithmStartTime); break;
case 1: algorithmStartTime = performance.now(); algorithmRunning = true; setSelectStatus(0); await quickSort(arrayToSort, 0, arrayToSort.length - 1); setSelectStatus(1); algorithmRunning = false; algorithmEndTime = performance.now(); timeToExecute = (algorithmEndTime-algorithmStartTime); break;
case 2: algorithmStartTime = performance.now(); algorithmRunning = true; setSelectStatus(0); await insertionSort(arrayToSort, 0, arrayToSort.length); setSelectStatus(1); algorithmRunning = false; algorithmEndTime = performance.now(); timeToExecute = (algorithmEndTime-algorithmStartTime); break;
case 3: algorithmStartTime = performance.now(); algorithmRunning = true; setSelectStatus(0); await selectionSort(arrayToSort); setSelectStatus(1); algorithmRunning = false; algorithmEndTime = performance.now(); timeToExecute = (algorithmEndTime-algorithmStartTime); break;
case 4: algorithmStartTime = performance.now(); algorithmRunning = true; setSelectStatus(0); await mergeSort(arrayToSort, 0, arrayToSort.length); setSelectStatus(1); algorithmRunning = false; algorithmRunning = false; algorithmEndTime = performance.now(); timeToExecute = (algorithmEndTime-algorithmStartTime); break;
}
}
else{
alert("Can't run more than one instance of an algorithm!")
}
}
/*
FUNCTION: RESET
PARAMETERS: NONE
DESCRIPTION: RESETS THE CANVAS BY REFRESHING THE PAGE
*/
function reset(){
location.reload();
}
/*
FUNCTION: SLEEP
PARAMETERS: ms: The time to sleep in milliseconds
DESCRIPTION: SLEEPS EXECUTION WHEN CALLED
*/
function sleep(ms){
if(playbackSpeed != 0){
totalDelay += playbackSpeed;
return new Promise(resolve => setTimeout(resolve, ms));
}
}
/*
FUNCTION: SETSELECTSTATUS
PARAMETERS: status: enabled/disaabled
DESCRIPTION: ENABLES/DISABLES THE SELECTS
*/
function setSelectStatus(status){
var algorithmSelectBox = document.getElementById("algorithm-selection");
var speedSelectBox = document.getElementById("speed-selection");
if(status == 1){ //ENABLED
algorithmSelectBox.disabled = false;
speedSelectBox.disabled = false;
}
else if(status == 0){ //DISABLED
algorithmSelectBox.disabled = true;
speedSelectBox.disabled = true;
}
}