Answers for "merge sort js example"

5

merge sort javascript

// Merge Sort Implentation (Recursive)

function mergeSort (unsortedArray) {
  if (unsortedArray.length <= 1) {
    return unsortedArray;
  }
  
  const middle = Math.floor(unsortedArray.length / 2);

  const left = unsortedArray.slice(0, middle);
  const right = unsortedArray.slice(middle);

  // Using recursion to combine the left and right
  return merge(
    mergeSort(left),
    mergeSort(right)
  );
}

function merge (left, right) {
  let resultArray = [], leftIndex = 0, rightIndex = 0;

  while (leftIndex < left.length && rightIndex < right.length) {
    if (left[leftIndex] < right[rightIndex]) {
      resultArray.push(left[leftIndex]);
      leftIndex++;
    } else {
      resultArray.push(right[rightIndex]);
	  rightIndex++;
    }
  }

  return resultArray
          .concat(left.slice(leftIndex))
          .concat(right.slice(rightIndex));
}
Posted by: Guest on March-13-2020
5

array sorting javascript mergesort

// Merge Sort (Recursive)
function mergeSort (unsortedArray) {
  // No need to sort the array if the array only has one element or empty
  if (unsortedArray.length <= 1) {
    return unsortedArray;
  }
  // In order to divide the array in half, we need to figure out the middle
  const middle = Math.floor(unsortedArray.length / 2);

  // This is where we will be dividing the array into left and right
  const left = unsortedArray.slice(0, middle);
  const right = unsortedArray.slice(middle);

  // Using recursion to combine the left and right
  return merge(
    mergeSort(left), mergeSort(right)
  );
}
Posted by: Guest on September-04-2020
0

merge sort js example

function mergeSort(array, compare = (a, b) => a < b) {
  if (array.length < 2) { return array; }

  const middleIndex = Math.floor(array.length / 2);
  const head = mergeSort(array.slice(0, middleIndex), compare);
  const tail = mergeSort(array.slice(middleIndex), compare);

  const orderedArray = [];
  let tailIndex = 0;
  for (const element of head) {
    while(tailIndex < tail.length && compare(tail[tailIndex], element)) {
      orderedArray.push(tail[tailIndex++]);
    }
    orderedArray.push(element)
  }
  if (tailIndex < tail.length) {
    orderedArray.push(...tail.slice(tailIndex));
  }
  
  return orderedArray;
}
Posted by: Guest on August-10-2021
2

javascript merge sort

const merge = (left, right) => {
  const resArr = [];
  let leftIdx = 0;
  let rightIdx = 0;

  while (leftIdx < left.length && rightIdx < right.length) {
    left[leftIdx] < right[rightIdx]
      ? resArr.push(left[leftIdx++])
      : resArr.push(right[rightIdx++]);
  }
  return [...resArr, ...left.slice(leftIdx), ...right.slice(rightIdx)];
};

const mergeSort = arr =>
  arr.length <= 1
    ? arr
    : merge(
        mergeSort(arr.slice(0, Math.floor(arr.length / 2))),
        mergeSort(arr.slice(Math.floor(arr.length / 2)))
      );
Posted by: Guest on March-31-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language