Skip to content

Commit e54ab7c

Browse files
committed
Merge pull request algorithm-visualizer#3 from kciter/master
Add Mergesort
2 parents 2421d79 + 7a2f529 commit e54ab7c

File tree

4 files changed

+95
-2
lines changed

4 files changed

+95
-2
lines changed

algorithm/category.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"insertion": "Insertion Sort",
1919
"selection": "Selection Sort",
2020
"bubble": "Bubble Sort",
21-
"quick": "Quicksort"
21+
"quick": "Quicksort",
22+
"merge": "Mergesort"
2223
}
2324
},
2425
"etc": {
@@ -28,4 +29,4 @@
2829
"scratch_paper": "<i class='fa fa-code'></i> Scratch Paper"
2930
}
3031
}
31-
}
32+
}

algorithm/sorting/merge/basic/code.js

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
tracer._print('original array = [' + D.join(', ') + ']');
2+
tracer._sleep(1000);
3+
tracer._pace(500);
4+
5+
function mergeSort(start, end) {
6+
if (Math.abs(end - start) <= 1) {
7+
return [];
8+
}
9+
10+
var middle = Math.ceil((start + end) / 2);
11+
12+
mergeSort(start, middle);
13+
mergeSort(middle, end);
14+
15+
tracer._print('divide left[' + start + ', ' + (middle-1) + '], right[' + (middle) + ', ' + (end-1) + ']');
16+
17+
return mergeSort.merge(start, middle, end);
18+
}
19+
20+
mergeSort.merge = function (start, middle, end) {
21+
var left = Array();
22+
var right = Array();
23+
24+
var leftSize = middle - start;
25+
var rightSize = end - middle;
26+
var maxSize = Math.max(leftSize, rightSize);
27+
var size = end - start;
28+
var i;
29+
30+
for (i = 0; i < maxSize; i++) {
31+
if (i < leftSize) {
32+
left.push(D[start + i]);
33+
tracer._select(start + i);
34+
tracer._print('insert value into left array[' + i +'] = ' + D[start + i]);
35+
}
36+
if (i < rightSize) {
37+
right.push(D[middle + i]);
38+
tracer._select(middle + i);
39+
tracer._print('insert value into right array[' + i +'] = ' + D[middle + i]);
40+
}
41+
}
42+
tracer._print('left array = [' + left.join(', ') + '],' + 'right array = [' + right.join(', ') + ']');
43+
44+
i = 0;
45+
while (i < size) {
46+
if (left[0] && right[0]) {
47+
if (left[0] > right[0]) {
48+
D[start + i] = right.shift();
49+
tracer._print('rewrite from right array[' + i + '] = ' + D[start+i]);
50+
} else {
51+
D[start + i] = left.shift();
52+
tracer._print('rewrite from left array[' + i + '] = ' + D[start+i]);
53+
}
54+
} else if (left[0]) {
55+
D[start + i] = left.shift();
56+
tracer._print('rewrite from left array[' + i + '] = ' + D[start+i]);
57+
} else {
58+
D[start + i] = right.shift();
59+
tracer._print('rewrite from right array[' + i + '] = ' + D[start+i]);
60+
}
61+
62+
tracer._deselect(start + i);
63+
tracer._notify(start + i);
64+
65+
i++;
66+
}
67+
68+
tempArray = Array();
69+
for (i=start; i<end; i++) {
70+
tempArray.push(D[i]);
71+
}
72+
tracer._print('merged array = [' + tempArray.join(', ') + ']');
73+
}
74+
75+
mergeSort(0, D.length)
76+
tracer._print('sorted array = [' + D.join(', ') + ']');

algorithm/sorting/merge/basic/data.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var tracer = new Array1DTracer();
2+
var D = Array1D.random(15);
3+
tracer._setData(D);

algorithm/sorting/merge/desc.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Quicksort": "In computer science, merge sort (also commonly spelled mergesort) is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Mergesort is a divide and conquer algorithm that was invented by John von Neumann in 1945. A detailed description and analysis of bottom-up mergesort appeared in a report by Goldstine and Neumann as early as 1948.",
3+
"Complexity": {
4+
"time": "average O(n log n)",
5+
"space": "worst O(n)"
6+
},
7+
"References": [
8+
"<a href='https://en.wikipedia.org/wiki/Merge_sort'>Wikipedia</a>"
9+
],
10+
"files": {
11+
"basic": "Basic"
12+
}
13+
}

0 commit comments

Comments
 (0)