-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeSort.js
305 lines (270 loc) · 10.1 KB
/
mergeSort.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
// declare global variables
var arr = []
var steps = []
var length = 40;
var isSorted = false;
// store random value in arr[]
while (arr.length < length) {
var r = Math.floor(Math.random() * 200) + 1;
if (arr.indexOf(r) === -1)
arr.push(r);
}
var svg, barChart, text, xScale, yScale;
var durationTime = 150;
var chartWidth = 1400, chartHeight = 400, barPadding = 5;
var barWidth = chartWidth / arr.length;
// select environment
svg = d3.select('#canvas')
.attr('width', chartWidth)
.attr('height', chartHeight)
// xScale will help us set the x position of the bars
xScale = d3.scaleBand() //Ordinal scale
.domain([0, arr.length]) //sets the input domain for the scale
.range([0, barWidth])//enables rounding of the range
.paddingInner(barPadding); //spacing between each bar
//yScale will help us map data to the height of bars in the barchart
yScale = d3.scaleLinear()
.domain([0, d3.max(arr)])
.range([0, chartHeight - 28])
// generate bar and text present array's element values
barChart = svg.selectAll('g')
.data(arr)
.enter()
.append('g')
barChart.append('rect')
.attr('id', function (d) { return 'rect' + d })
.attr('x', function (d, i) {
return i * barWidth;
})
.attr('y', function (d) {
return chartHeight - yScale(d);
})
.attr('height', function (d) {
return yScale(d);
})
.attr('width', barWidth - barPadding)
.attr('fill', getRandomColor())
.attr('transform', function (d, i) {
return 'translate(0, 0)';
});
barChart.append('text')
.attr('id', function (d) { return 'text' + d })
.html(function (d) { return d; })
.text(function (d) {
return d;
})
.attr('y', function (d, i) {
return chartHeight - yScale(d) - 7;
})
.attr('x', function (d, i) {
return barWidth * i + 5;
})
.attr('fill', 'black');
// reset function returns new array
function reset() {
// document.getElementById("sortBtn").disabled = false;
isSorted = false;
var newText1 = d3.select('#info').text("Data is unsorted");
arr = [];
steps = [];
while (arr.length < length) {
var r = Math.floor(Math.random() * 200) + 1;
if (arr.indexOf(r) === -1)
arr.push(r);
}
barChart.data(arr);
barChart.select('rect')
.transition().duration(durationTime)
.attr('id', function (d) { return 'rect' + d })
.attr('x', function (d, i) {
return i * barWidth;
})
.attr('y', function (d) {
return chartHeight - yScale(d);
})
.attr('height', function (d) {
return yScale(d);
})
.attr('fill', getRandomColor())
.attr('transform', function (d, i) {
return 'translate(0, 0)';
})
barChart.select('text')
.transition().duration(durationTime)
.attr('id', function (d) { return 'text' + d })
.text(function (d) {
return d;
})
.attr('y', function (d, i) {
return chartHeight - yScale(d) - 7;
})
.attr('x', function (d, i) {
return barWidth * i + 5;
})
.attr('transform', function (d, i) {
return 'translate(0, 0)';
})
return arr;
}
// generate random color for bar chart
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
// merge sort algorithm
function mergeSort(arr) {
// recursion base case
// it checks if the array length is less than or equal to 1.
// if that's the case return the arr else keep splicing.
// console.log("CHECK3333: ", arr);
if (arr.length <= 1) { return arr;}
// remember that we said merge sort uses divide and conquer
// algorithm pattern
// it firsts know the half point of the array.
let halfPoint = Math.ceil(arr.length / 2);
// and then splice the array from the beginning up to the half point.
// but for the fact that merge sort needs the array to be of one element, it will keep splicing that half till it fulfills the condition of having one element array.
let firstHalf = mergeSort(arr.splice(0, halfPoint));
// second array from the half point up to the end of the array.
let secondHalf = mergeSort(arr.splice(-halfPoint));
// merge the array back and return the result.
// note that we are using the helper function we created above.
return merge(firstHalf, secondHalf);
function merge(arr1, arr2) {
let result = []; // the array to hold results.
let i = 0;
let j = 0;
steps.push(arr1);
steps.push(arr2);
// as the pseudo-code implies, we have to loop through the
// arrays at the same time and it has to be done once.
// note that if one array completes its iteration, we will
// have to stop the while loop.
while (i < arr1.length && j < arr2.length) {
// compare the elements one at a time.
if (arr1[i] > arr2[j]) {
result.push(arr2[j]);
j++;
} else {
result.push(arr1[i]);
i++;
}
}
// these other while loops checks if there's some item left
// in the arrays so that we can push their elements in the result array.
while (i < arr1.length) {
result.push(arr1[i]);
i++;
}
while (j < arr2.length) {
result.push(arr2[j]);
j++;
}
return result;
}
}
// animation function
async function animate(steps) {
var counter = 0, left, right, x1, x2;
var color = getRandomColor();
while (counter < steps.length - 1) {
var newText2 = d3.select('#info').text('Sorting data...');
left = steps[counter];
counter++;
right = steps[counter]
counter++;
var i = 0;
var j = 0;
var numSorted = 0;
// Farthest left is always the starting point
x1 = parseInt(d3.select('#rect' + left[0]).attr('x'));
for (let i = 1; i < left.length; i++) {
if (parseInt(d3.select('#rect' + left[i]).attr('x')) < x1) {
x1 = parseInt(d3.select('#rect' + left[i]).attr('x'));
}
}
// compare and swap bars' positions if needed
while (i < left.length && j < right.length) {
if (left[i] > right[j]) {
x2 = d3.select('#rect' + right[j]).attr('x');
await d3.select('#rect' + right[j])
.transition().duration(durationTime)
.attr('transform', function (d) { return 'translate(' + (parseInt(x1) + (barWidth * numSorted) - parseInt(x2)) + ', 0)' })
.attr('fill', 'cyan')
.transition().duration(durationTime)
.attr('fill', color)
.each(function () {
d3.select('#text' + right[j])
.transition().duration(durationTime)
.attr('transform', function (d) { return 'translate(' + (parseInt(x1) + (barWidth * numSorted) - parseInt(x2)) + ', 0)' })
})
.end()
j++;
numSorted++;
} else {
x2 = d3.select('#rect' + left[i]).attr('x');
await d3.select('#rect' + left[i]).transition()
.duration(durationTime)
.attr('transform', function (d) { return 'translate(' + (parseInt(x1) + (barWidth * numSorted) - parseInt(x2)) + ', 0)' })
.attr('fill', 'cyan')
.transition().duration(durationTime)
.attr('fill', color)
.each(function () {
d3.select('#text' + left[i])
.transition().duration(durationTime)
.attr('transform', function (d) { return 'translate(' + (parseInt(x1) + (barWidth * numSorted) - parseInt(x2)) + ', 0)' })
})
.end()
i++;
numSorted++;
}
}
while (i < left.length) {
x2 = d3.select('#rect' + left[i]).attr('x');
await d3.select('#rect' + left[i])
.transition().duration(durationTime)
.attr('transform', function (d) { return 'translate(' + (parseInt(x1) + (barWidth * numSorted) - parseInt(x2)) + ', 0)' })
.attr('fill', 'cyan')
.transition().duration(durationTime)
.attr('fill', color)
.each(function () {
d3.select('#text' + left[i])
.transition().duration(durationTime)
.attr('transform', function (d) { return 'translate(' + (parseInt(x1) + (barWidth * numSorted) - parseInt(x2)) + ', 0)' })
})
.end()
i++;
numSorted++;
}
while (j < right.length) {
x2 = d3.select('#rect' + right[j]).attr('x');
await d3.select('#rect' + right[j])
.transition().duration(durationTime)
.attr('transform', function (d) { return 'translate(' + (parseInt(x1) + (barWidth * numSorted) - parseInt(x2)) + ', 0)' })
.attr('fill', 'cyan')
.transition().duration(durationTime)
.attr('fill', color)
.each(function () {
d3.select('#text' + right[j])
.transition().duration(durationTime)
.attr('transform', function (d) { return 'translate(' + (parseInt(x1) + (barWidth * numSorted) - parseInt(x2)) + ', 0)' })
})
.end()
j++;
numSorted++;
}
}
var newText3 = d3.selectAll('#info').text('Data is sorted');
}
function visualization() {
if (isSorted)
return;
mergeSort(arr);
animate(steps);
isSorted = true;
// document.getElementById("sortBtn").disabled = true;
}