-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting.js
168 lines (150 loc) · 4.89 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
function SortingVisualizer() {
var canvas, selection, bubble, quick, insertion, arr, color, current, speed, speedVal, barWidth, offsetStep;
this.init = function() {
canvas = document.getElementById("canvas");
selection = document.getElementById("selection");
bubble = document.getElementById("bubble");
quick = document.getElementById("quick");
insertion = document.getElementById("insertion");
bubble.addEventListener("click", bubbleSort);
selection.addEventListener("click", selectionSort);
quick.addEventListener("click", quickSort);
insertion.addEventListener("click", insertionSort);
color = ["red", "green", "yellow", "blue", "pink", "purple", "peru", "palegreen", "silver", "snow", "goldenrod"];
arr = document.getElementById("arr");
speed = document.getElementById("speed");
speed.addEventListener("input", changeSpeed);
speedVal = 100;
barWidth = 10;
offsetStep = 0.5;
};
var changeSpeed = function(evt) {
speedVal = parseInt(evt.target.value);
}
var getRandomColor = function() {
return color[Math.floor(Math.random() * color.length)];
};
var getArray = function() {
var ar = arr.value;
ar = ar.split(" ");
var res = [];
for (let el of ar) {
if (el !== "") {
res.push(parseInt(el));
}
}
if (res.length === 0) {
alert("enter a valid size");
return [];
}
var sz = res[0];
barWidth = Math.floor((canvas.width) * 0.95 / sz);
offsetStep = ((canvas.width) * 0.05 / sz);
res = [];
for (var i=0; i<sz; i++) {
res.push(20 + Math.floor(Math.random() * 550));
}
return res;
};
var selectionSort = function() {
var arr = getArray();
current = "selection";
startSelectionSort(arr, 0);
};
var startSelectionSort = function (arr, i) {
if (i == arr.length || current !== "selection") return;
var minInd = i;
for (var j=i+1; j<arr.length; j++) {
if (arr[j] < arr[minInd]) {
minInd = j;
}
}
var tmp = arr[minInd];
arr[minInd] = arr[i];
arr[i] = tmp;
draw(arr);
setTimeout(startSelectionSort, speedVal, arr, i+1);
};
var bubbleSort = function() {
var arr = getArray();
current = "bubble";
startBubbleSort(arr, arr.length-1);
}
var startBubbleSort = function(arr, i) {
if (i == -1 || current !== "bubble") return;
var maxInd = i;
for (var j=i-1; j>=0; j--) {
if (arr[j] > arr[maxInd]) {
maxInd = j;
}
}
var tmp = arr[maxInd];
arr[maxInd] = arr[i];
arr[i] = tmp;
draw(arr);
setTimeout(startBubbleSort, speedVal, arr, i-1);
}
var quickSort = function() {
var arr = getArray();
current = "quick";
startQuickSort(arr, 0, arr.length-1);
};
var startQuickSort = function(arr, l, r) {
if (l >= r || current != "quick") return;
var mid = partition(arr, l, r);
draw(arr);
setTimeout(startQuickSort, speedVal, arr, l, mid-1);
setTimeout(startQuickSort, speedVal, arr, mid+1, r);
}
var swap = function(arr ,i, j) {
var tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
};
var partition = function(arr, l, r) {
var randInd = l + Math.floor(Math.random() * (r-l));
swap(arr, l, randInd);
var piv = arr[l];
var ind = l+1;
for (var j=l+1; j<=r; j++) {
if (piv >= arr[j]) {
swap(arr, ind, j);
ind += 1;
}
}
swap(arr, ind-1, l);
return ind-1;
}
var insertionSort = function() {
var arr = getArray();
current = "insertion";
startInsertionSort(arr, 1);
}
var startInsertionSort = function(arr, ind) {
if (current !== 'insertion' || ind >= arr.length) return;
var cur = ind-1;
var el = arr[ind];
for (cur=ind-1; cur >= 0; cur--) {
if (arr[cur] < el) {
break;
}
arr[cur+1] = arr[cur];
}
arr[cur+1] = el;
draw(arr);
setTimeout(startInsertionSort, speedVal, arr, ind+1);
}
draw = function(arr) {
var pen = canvas.getContext("2d");
pen.clearRect(0, 0, canvas.width, canvas.height);
var offset = 0;
//use ctx.save and restore carefully
for (var i=0; i<arr.length; i++) {
let randomColor = getRandomColor();
pen.fillStyle = randomColor;
pen.fillRect(offset, canvas.height-arr[i], barWidth, arr[i]);
offset += barWidth + offsetStep;
}
pen.restore();
}
}