MCPcopy Create free account
hub / github.com/davidshariff/computer-science / MergeSort

Function MergeSort

Sorting/MergeSort.js:44–70  ·  view source on GitHub ↗
(inputArr)

Source from the content-addressed store, hash-verified

42
43 },
44 MergeSort = function(inputArr) {
45
46 var len = inputArr.length,
47 left = [],
48 right = [],
49 mid;
50
51 // less than 2 elements means we can't merge / compare anything
52 if (len < 2) {
53 return inputArr;
54 }
55
56 // find the middle, rounding up for odd no of elems
57 mid = Math.floor(len / 2);
58
59 // split left and right into sub-arrays
60 left = inputArr.slice(0, mid);
61 right = inputArr.slice(mid);
62
63 // keep dividing the sub-arrays
64 left = MergeSort(left);
65 right = MergeSort(right);
66
67 // conquer the sub-arrays by sorting and merging them
68 return Merge(left, right);
69
70 };
71
72 var myArray = [2, 4, 1, 6, 8, 5, 9, 3, 4];
73

Callers 1

MergeSort.jsFile · 0.85

Calls 1

MergeFunction · 0.85

Tested by

no test coverage detected