Skip to content

Instantly share code, notes, and snippets.

@vaidehijoshi
Last active November 27, 2022 20:17
Show Gist options
  • Save vaidehijoshi/ee121ec28c0ad1382a6bb68957a3b7e0 to your computer and use it in GitHub Desktop.
Save vaidehijoshi/ee121ec28c0ad1382a6bb68957a3b7e0 to your computer and use it in GitHub Desktop.
function mergeSort(array) {
// Determine the size of the input array.
var arraySize = array.length;
// If the array being passed in has only one element
// within it, it is considered to be a sorted array.
if (arraySize === 1) {
return;
}
// If array contains more than one element,
// split it into two parts (left and right arrays).
var midpoint = Math.floor(arraySize / 2);
var leftArray = array.slice(0, midpoint);
var rightArray = array.slice(midpoint);
// Recursively call mergeSort() on
// leftArray and rightArray sublists.
mergeSort(leftArray);
mergeSort(rightArray);
// After the mergeSort functions above finish executing,
// merge the sorted leftArray and rightArray together.
merge(leftArray, rightArray, array);
// Return the fully sorted array.
return array;
}
function merge(leftArray, rightArray, array) {
var index = 0;
while (leftArray.length && rightArray.length) {
console.log('array is: ', array);
if (rightArray[0] < leftArray[0]) {
array[index++] = rightArray.shift();
} else {
array[index++] = leftArray.shift();
}
}
while (leftArray.length) {
console.log('left array is: ', leftArray);
array[index++] = leftArray.shift();
}
while (rightArray.length) {
console.log('right array is: ', rightArray);
array[index++] = rightArray.shift();
}
console.log('** end of merge function ** array is: ', array);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment