forked from sahdev50/SortingAlgorithmVisualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
251 lines (197 loc) · 8.17 KB
/
app.js
File metadata and controls
251 lines (197 loc) · 8.17 KB
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
//var x = document.getElementById("bar-numbers").value;
var count = 1 + 50, //count = 1 + x;
durationTime = 2000/15, // durationTime = 2000/(count/3);
array = d3.shuffle(d3.range(1,count)),
unsortedArray = [...array],
sortedArray = [],
stop = false;
var margin = {top: 40, right: 40, bottom: 180, left: 40},
width = 760 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
var barWidth = width/count;
var x = d3.scaleLinear()
.domain([0,count])
.range([0, width]);
var svg = d3.select("#array").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + 0 + ")")
var rects = svg.append("g")
.attr("transform", "translate(" + barWidth + ",2)")
.selectAll("rect")
.data(unsortedArray)
.enter().append("rect")
rects.attr("id", function(d) {return "rect" + d})
.attr("transform", function(d, i) {return "translate(" + (x(i) - barWidth) + ",0)"})
.attr("width", barWidth *.9)
.attr("height", function(d) {return d*barWidth/3})
function reset() {
document.getElementById("text").innerHTML = "_Sorting - Visualizer_";
unsortedArray = [...array];
sortedArray = [];
stop = false;
rects.attr("class", "")
.transition().duration(2000)
.attr("transform", function(d, i) {return "translate(" + (x(i-1)) + ", 0)"})
}
function slide(d, i) {
d3.select("#rect" + d)
.transition().duration(durationTime)
.attr("transform", function(d) {return "translate(" + (x(i-1)) + ", 0)"})
}
function insertionSort() {
document.getElementById("text").innerHTML = "_Insertion - Sort_";
var value = unsortedArray.shift();
sortedArray.push(value);
reArrange(sortedArray.length-1);
function reArrange(n) {
if (stop) return stop = false;
d3.selectAll("rect").attr("class", "")
d3.select("#rect" + value).attr("class", "testing")
if (n > 0 && sortedArray[n-1] > value) {
d3.timeout(function() {
sortedArray.splice(n,1);
sortedArray.splice(n-1,0,value);
slide(sortedArray[n], n);
slide(sortedArray[n-1], n-1);
reArrange(--n)
}, durationTime * 2);
} else if (unsortedArray.length) {
d3.timeout(function() {insertionSort()}, durationTime * 2);
} else {
return d3.selectAll("rect").attr("class", "")
}
}
}
function selectionSort() {
document.getElementById("text").innerHTML = "_Selection - Sort_";
var min = count,
spliceIndex,
i = 0;
function findMin() {
if (stop) return stop = false;
d3.timeout(function() {
if (i<=unsortedArray.length) {
d3.select("#rect" + unsortedArray[i]).attr("class", "testing")
d3.timeout(function() {
d3.select("#rect" + unsortedArray[i]).attr("class", "")
if (unsortedArray[i] < min) {
d3.select("#rect" + unsortedArray[i]).attr("class", "min")
d3.select("#rect" + min).attr("class", "")
min = unsortedArray[spliceIndex = i]
}
i++;
d3.timeout(function() {return findMin()}, durationTime/2);
}, durationTime/2);
} else {
sortedArray.push(min);
unsortedArray.splice(spliceIndex,1);
rects.transition().duration(durationTime * 4)
.attr("transform", function(d) {
var xVal = sortedArray.indexOf(d) > -1 ? sortedArray.indexOf(d) : unsortedArray.indexOf(d) + sortedArray.length;
return "translate(" + x(xVal - 1) + ",0)"
})
rects.attr("class", "")
d3.timeout(function() {
if (unsortedArray.length > 0) selectionSort();
}, durationTime);
return;
}
})
}
findMin();
}
function bubbleSort() {
document.getElementById("text").innerHTML = "_Bubble - Sort_";
function sortPass(i) {
if (!unsortedArray.length || stop) return stop = false
if (i<=unsortedArray.length) {
if (unsortedArray[i] < unsortedArray[i-1]) {
d3.select("#rect" + unsortedArray[i]).attr("class", "testing")
d3.select("#rect" + unsortedArray[i-1]).attr("class", "min")
d3.timeout(function() {
d3.select("#rect" + unsortedArray[i]).attr("class", "")
d3.select("#rect" + unsortedArray[i-1]).attr("class", "")
}, durationTime);
var temp = unsortedArray[i-1];
unsortedArray[i-1] = unsortedArray[i];
unsortedArray[i] = temp;
slide(unsortedArray[i], i + sortedArray);
slide(unsortedArray[i-1], i-1 + sortedArray);
d3.timeout(function() {return sortPass(++i)}, durationTime);
} else if (i == unsortedArray.length) {
for (n = i; n == unsortedArray[n-1]; n--) {
unsortedArray.pop();
}
sortPass(++i);
} else {
sortPass(++i);
}
} else {
bubbleSort();
}
}
sortPass(1);
}
function mergeSort() {
document.getElementById("text").innerHTML = "_Merge - Sort_";
var mergeReps = (unsortedArray.length).toString(2).length + 1;
var mergeArrays = [[...unsortedArray], []];
for (i=0; i<unsortedArray.length; i += 2) {
mergeArrays[1].push(mergeTwo([unsortedArray[i]], [unsortedArray[i+1]]))
}
for (n=2; n<mergeReps; n++) {
mergeArrays[n] = [];
var unMerged = mergeArrays[n-1];
for (i=0; i<unMerged.length; i += 2) {
mergeArrays[n].push(mergeTwo(unMerged[i], unMerged[i+1] ? unMerged[i+1] : []))
}
}
for (i=1; i<mergeArrays.length; i++) {
mergeArrays[i] = d3.merge(mergeArrays[i])
}
mergeMove(0);
function mergeTwo(iArray, nArray) {
var newArray = [];
for (var i=0, n=0; i<iArray.length || n<nArray.length;) {
if (iArray[i] < nArray[n]) {
newArray.push(iArray[i++])
} else if (iArray[i] > nArray[n]) {
newArray.push(nArray[n++])
} else if (!(iArray[i])) {
newArray.push(nArray[n++])
} else if (!(nArray[n])) {
newArray.push(iArray[i++])
}
}
return newArray;
}
function mergeMove(j) {
var oldArray = mergeArrays[j],
newArray = [...mergeArrays[j+1]],
sortedArray = [];
moveStep(0);
function moveStep(n) {
if (stop) return stop = false;
d3.selectAll("rect").attr("class", "")
d3.select("#rect" + newArray[n]).attr("class", "testing")
sortedArray.push(newArray[n])
oldArray.shift()
rects.transition().duration(durationTime)
.attr("transform", function(d) {
var xVal = sortedArray.indexOf(d) > -1 ? sortedArray.indexOf(d) : oldArray.indexOf(d) + sortedArray.length;
return "translate(" + x(xVal - 1) + ",0)"
})
d3.timeout(function() {
if (oldArray.length > 0) {
moveStep(++n)
} else if (mergeArrays[j + 2]) {
mergeMove(++j)
} else {
rects.classed("testing", false)
}
}, durationTime);
}
}
}