-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (129 loc) · 3.23 KB
/
index.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
let values = [];
var shuffleButton;
var bubbleSortButton;
var quickSortButton;
var mergeSortButton;
function setup() {
// Create a canvas to display UI
const canvas = createCanvas(600, 345);
// Randomize elements
shufflearray();
// UI elements
shuffleButton = createButton('Shuffle');
shuffleButton.position(20, 325);
shuffleButton.mousePressed(shufflearray);
bubbleSortButton = createButton('Bubble Sort');
bubbleSortButton.position(120, 325);
bubbleSortButton.mousePressed(bubblesort);
quickSortButton = createButton('Quick Sort');
quickSortButton.position(220, 325);
quickSortButton.mousePressed(callQuickSort);
mergeSortButton = createButton('Merge Sort');
mergeSortButton.position(320, 325);
mergeSortButton.mousePressed(callMergeSort);
}
function draw() {
// Background black
background(0);
// Draw bars to visual represent array elements
for (let i = 0; i < 60; i++) {
rect(i * 10, 300 - values[i], 10, 10 + values[i]);
}
}
function callQuickSort() {
// Since mousePressed takes no paramters, this function acts as an intermediary function
quicksort(values, 0, values.length-1);
}
function callMergeSort() {
// Array based implentation of mergesort
mergesort(values, 0, values.length - 1);
}
async function mergesort(arr, left, right) {
if (left < right) {
let middle = floor((left + right) / 2);
await mergesort(arr, left, middle);
await mergesort(arr, middle + 1, right);
await merge(arr, left, middle, right);
}
}
async function merge(arr, left, middle, right) {
// // Create temp arrays
let L = subset(arr, left, middle - left + 1);
let R = subset(arr, middle + 1, right - middle);
let i =0;
let j = 0;
let k = left;
// Merge the temp arrays
while(i < L.length && j < R.length) {
if(L[i] <= R[j]) {
await sleep(10);
arr[k] = L[i];
i++
} else {
await sleep(10);
arr[k] = R[j];
j++;
}
k++;
}
// Copy remaining elements of L[] if any
while (i < L.length) {
await sleep(10);
arr[k] = L[i];
i++;
k++;
}
// Copy remaining elements of R[] if any
while (j < R.length) {
await sleep(10);
arr[k] = R[j];
j++;
k++;
}
}
async function quicksort(arr, start, end) {
if(start < end) {
let index = await partition(arr, start, end);
await quicksort(arr, start, index-1);
await quicksort(arr, index+1, end);
}
}
async function partition(arr, start, end) {
let index = start;
let pivot = arr[end];
for(let i = start; i < end; i++) {
if(arr[i] < pivot) {
await sleep(10);
await swap(arr, i, index);
index++;
}
}
await swap(arr, index, end);
return index;
}
async function bubblesort() {
let swapped = false;
do {
swapped = false;
for (let i = 1; i < values.length; i++) {
if (values[i] < values[i - 1]) {
await sleep(10);
await swap(values, i, i-1);
swapped = true;
}
}
} while (swapped == true);
}
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function shufflearray() {
for (let i = 0; i < 60; i++) {
values[i] = random(0, 100);
}
}
async function swap(arr, i, j) {
let tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}