-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort-visualizer.html
287 lines (237 loc) · 6.42 KB
/
sort-visualizer.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
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
<!DOCTYPE html><html>
<script src="Chart.js-master/Chart.js"></script>
<body>
<canvas id="Numbers" width="1000" height="1000"></canvas>
<div style="padding 10px 0px 0px 0px">
<p id="inspections">Reads: 0</p>
</div>
<div style="padding 10px 0px 0px 0px">
<p id="swaps">Swaps: 0</p>
</div>
<div style="padding: 20px 0px 0px 0px">
<button type="button" onclick="onShuffle()">Shuffle</button>
</div>
<div style="padding: 50px 0px 0px 0px">
<button type="button" onclick="onBubbleSort()">Bubble Sort</button>
</div>
<div style="padding: 50px 0px 0px 0px">
<button type="button" onclick="onInsertionSort()">Insertion Sort</button>
</div>
<div style="padding: 50px 0px 0px 0px">
<button type="button" onclick="onSelectionSort()">Selection Sort</button>
</div>
<div style="padding: 50px 0px 0px 0px">
<button type="button" onclick="onQuickSort()">Quicksort</button>
</div>
<script>
var ctx = document.getElementById("Numbers").getContext("2d");
var data = {
labels: [],
datasets: [
{
label: "My Second dataset",
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,0.8)",
highlightFill: "rgba(151,187,205,0.75)",
highlightStroke: "rgba(151,187,205,1)",
data: []
}
]
};
for (var i = 0; i < 50; ++i){
data.labels.push(i+1);
data.datasets[0].data.push(i+1);
}
shuffle(data.datasets[0].data);
var randChart = new Chart(ctx).Bar(data, {animationSteps:2});
function shuffle(list){
for (var i = 0; i < list.length; ++i){
var randIdx = Math.floor(Math.random() * (list.length - i)) + i;
// console.log(i);
// console.log("index " + (i) + ": " + randIdx);
swap(list, i, randIdx);
}
}
function shuffleChart(list){
for (var i = 0; i < list.length; ++i){
var randIdx = Math.floor(Math.random() * (list.length - i)) + i;
// console.log(i);
// console.log("index " + (i) + ": " + randIdx);
swapElement(list, i, randIdx);
}
}
function colorRed(a, b){
randChart.datasets[0].bars[a].fillColor = "rgba(255,0,0,0.5)";
randChart.datasets[0].bars[b].fillColor = "rgba(255,0,0,0.5)";
}
function colorGreen(a, b){
randChart.datasets[0].bars[a].fillColor = "rgba(10,255,0,0.5)";
randChart.datasets[0].bars[b].fillColor = "rgba(10,255,0,0.5)";
}
function swap(list, a, b){
if (a == b) return;
list[a] = list[a] ^ list[b];
list[b] = list[a] ^ list[b];
list[a] = list[a] ^ list[b];
}
function swapElement(list, a, b){
if (a == b) return;
list[a].value = list[a].value ^ list[b].value;
list[b].value = list[a].value ^ list[b].value;
list[a].value = list[a].value ^ list[b].value;
}
function resetAllColors(){
for (var i = 0; i < randChart.datasets[0].bars.length; i++){
randChart.datasets[0].bars[i].fillColor = "rgba(151,187,205,0.5)";
}
}
function copyChartList(charList){
var retval = [];
for (var i = 0; i < randChart.datasets[0].bars.length; ++i){
retval.push(randChart.datasets[0].bars[i].value);
}
return retval;
}
function bubbleSort(list){
var copiedList = copyChartList(list);
var moveList = [];
var swapped = true;
var end = copiedList.length;
while (swapped === true){
swapped = false;
for (var i = 1; i < end; ++i){
if (copiedList[i-1] > copiedList[i]){
swap(copiedList, i-1, i);
moveList.push({type: "swap", a: i-1, b: i});
swapped = true;
} else {
moveList.push({type: "inspect", a: i-1, b: i});
}
}
end--;
}
return moveList;
}
function insertionSort(list){
var copiedList = copyChartList(list);
var moveList = [];
for (var i = 0; i < copiedList.length; ++i){
var j = i;
while (true){
if (j > 0 && copiedList[j-1] > copiedList[j]){
swap(copiedList, j-1, j);
moveList.push({type: "swap", a: j-1, b: j});
j--;
} else {
moveList.push({type: "inspect", a: j-1, b: j});
break;
}
}
}
return moveList;
}
function selectionSort(list){
var copiedList = copyChartList(list);
var moveList = [];
for (var i = 0; i < copiedList.length; ++i){
var minIdx = i;
var min = copiedList[i];
for (var j = i; j < copiedList.length; ++j){
moveList.push({type: "inspect", a: minIdx, b: j});
if (copiedList[j] < min){
min = copiedList[j];
minIdx = j;
}
}
moveList.push({type: "swap", a: i, b: minIdx});
swap(copiedList, i, minIdx);
}
return moveList;
}
function quickSort(list){
var copiedList = copyChartList(list);
var moveList = [];
var sortStack = [{beg: 0, end: list.length-1}];
while (sortStack.length > 0){
var move = sortStack.shift();
if (move.beg >= move.end){
continue;
}
var pivot = Math.floor((move.beg + move.end) / 2);
var pivotVal = copiedList[pivot];
var swapPos = move.beg;
swap(copiedList, pivot, move.end);
moveList.push({type: "swap", a: pivot, b: move.end});
// Partition
for (var i = move.beg; i < move.end; ++i){
if (copiedList[i] <= pivotVal){
swap(copiedList, swapPos, i);
moveList.push({type: "swap", a: swapPos, b: i});
swapPos++;
} else {
moveList.push({type: "inspect", a: i, b: move.end});
}
}
swap(copiedList, swapPos, move.end);
moveList.push({type: "swap", a: swapPos, b: move.end});
sortStack.push({beg:move.beg, end: swapPos-1});
sortStack.push({beg:swapPos+1, end: move.end});
}
return moveList;
}
function playMoves(moveList){
var delay = 10;
var delta = 10;
for (var i = 0; i < moveList.length; ++i){
var move = moveList[i];
setTimeout(function(mv){
return function(){
resetAllColors();
if (mv.type === "swap"){
swapElement(randChart.datasets[0].bars, mv.a, mv.b);
colorRed(mv.a, mv.b)
} else if (mv.type === "inspect"){
colorGreen(mv.a, mv.b);
}
randChart.update();
}
}(move), delay);
delay += delta;
}
}
function setLabels(moveList){
var swapCount = 0, inspectionCount = 0;
for (var i = 0; i < moveList.length; ++i){
if (moveList[i].type === "inspect"){
inspectionCount++;
} else if (moveList[i].type === "swap"){
swapCount++;
}
}
document.getElementById("inspections").innerHTML = "Reads: " + inspectionCount;
document.getElementById("swaps").innerHTML = "Swaps: " + swapCount;
}
function onBubbleSort(){
sort(bubbleSort);
}
function onInsertionSort(){
sort(insertionSort);
}
function onSelectionSort(){
sort(selectionSort);
}
function onQuickSort(){
sort(quickSort);
}
function sort(sortFunc){
var moveList = sortFunc(randChart.datasets[0].bars);
playMoves(moveList);
randChart.update();
setLabels(moveList);
}
function onShuffle(){
shuffleChart(randChart.datasets[0].bars);
randChart.update();
}
</script>
</body></html>