showing results for - "implement a merge sort algorithm js"
Lino
01 Sep 2018
1// Merge Sort Implentation (Recursive)
2
3function mergeSort (unsortedArray) {
4  if (unsortedArray.length <= 1) {
5    return unsortedArray;
6  }
7  
8  const middle = Math.floor(unsortedArray.length / 2);
9
10  const left = unsortedArray.slice(0, middle);
11  const right = unsortedArray.slice(middle);
12
13  // Using recursion to combine the left and right
14  return merge(
15    mergeSort(left),
16    mergeSort(right)
17  );
18}
19
20function merge (left, right) {
21  let resultArray = [], leftIndex = 0, rightIndex = 0;
22
23  while (leftIndex < left.length && rightIndex < right.length) {
24    if (left[leftIndex] < right[rightIndex]) {
25      resultArray.push(left[leftIndex]);
26      leftIndex++;
27    } else {
28      resultArray.push(right[rightIndex]);
29	  rightIndex++;
30    }
31  }
32
33  return resultArray
34          .concat(left.slice(leftIndex))
35          .concat(right.slice(rightIndex));
36}
Flynn
11 Feb 2017
1// Merge Sort (Recursive)
2function mergeSort (unsortedArray) {
3  // No need to sort the array if the array only has one element or empty
4  if (unsortedArray.length <= 1) {
5    return unsortedArray;
6  }
7  // In order to divide the array in half, we need to figure out the middle
8  const middle = Math.floor(unsortedArray.length / 2);
9
10  // This is where we will be dividing the array into left and right
11  const left = unsortedArray.slice(0, middle);
12  const right = unsortedArray.slice(middle);
13
14  // Using recursion to combine the left and right
15  return merge(
16    mergeSort(left), mergeSort(right)
17  );
18}
Carlos
10 Jan 2020
1function merge(left, right) {
2  const resultArray = []
3  let leftIndex = 0
4  let rightIndex = 0
5
6  while (leftIndex < left.length && rightIndex < right.length) {
7    if (left[leftIndex] < right[rightIndex]) {
8      resultArray.push(left[leftIndex])
9      leftIndex+=1
10    } else {
11      resultArray.push(right[rightIndex])
12      rightIndex+=1
13    }
14  }
15  return resultArray
16    .concat(left.slice(leftIndex))
17    .concat(right.slice(rightIndex))
18}
19function mergeSort(unsortedArray) {
20  if (unsortedArray.length <= 1) {
21    return unsortedArray;
22  }
23  const middle = Math.floor(unsortedArray.length / 2);
24  const left = unsortedArray.slice(0, middle);
25  const right = unsortedArray.slice(middle);
26  return merge(
27    mergeSort(left), mergeSort(right)
28  );
29}
30mergeSort([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])
Lucas
07 Apr 2017
1function mergeSort(array, compare = (a, b) => a < b) {
2  if (array.length < 2) { return array; }
3
4  const middleIndex = Math.floor(array.length / 2);
5  const head = mergeSort(array.slice(0, middleIndex), compare);
6  const tail = mergeSort(array.slice(middleIndex), compare);
7
8  const orderedArray = [];
9  let tailIndex = 0;
10  for (const element of head) {
11    while(tailIndex < tail.length && compare(tail[tailIndex], element)) {
12      orderedArray.push(tail[tailIndex++]);
13    }
14    orderedArray.push(element)
15  }
16  if (tailIndex < tail.length) {
17    orderedArray.push(...tail.slice(tailIndex));
18  }
19  
20  return orderedArray;
21}
Fawn
13 Jun 2019
1const merge = (left, right) => {
2  const resArr = [];
3  let leftIdx = 0;
4  let rightIdx = 0;
5
6  while (leftIdx < left.length && rightIdx < right.length) {
7    left[leftIdx] < right[rightIdx]
8      ? resArr.push(left[leftIdx++])
9      : resArr.push(right[rightIdx++]);
10  }
11  return [...resArr, ...left.slice(leftIdx), ...right.slice(rightIdx)];
12};
13
14const mergeSort = arr =>
15  arr.length <= 1
16    ? arr
17    : merge(
18        mergeSort(arr.slice(0, Math.floor(arr.length / 2))),
19        mergeSort(arr.slice(Math.floor(arr.length / 2)))
20      );
queries leading to this page
merge sorted arrays jsmerge sort javascirptmergesort javascripthow to sort an array of strings using merge sort javascriptfastest merge sort jsmerge sort program in javascriptjs merge sort functionhow to merge sort in jssort concat jsmerge sort in jsmerge sort javascriptmerge sort javascript hoaresorting merge sort in jsmerge array sortmerge array sorted javascriptmergesort em jsmerge short javascriptmerge sorting using javascriptmerge sort in javasriptmerge algorithm javascriptmerge sort algorithm example in javascript 2cerge sort jsarray sort js merge sortmerge sort with javascriptjavascript merge sorted arraysnode js merge sortimplement merge sort javascriptpmergew saoprt jsimplementing merge sort using javascriptmerge sort logic in jsmergesor jsmerge sort injsimplement a merge sort algorithm jshow to implement merge sort javascriptjavascript merge sort in one functionmerge sort js 27javascript merge two sorted arraysmerge sort code jsmerge sorted arrays in order javascriptmerge sort javascriptmerge sort js loopmerge sort jsmerge sort in javvascripitarray sortjs mergesortmerge sorted array javascriptmergesort javasciirptjs concat and sortmergesort in javascriptmerge sort array javascriptmerge sort in java scriptmerge sort explained javascriptmaking merge sort javascriptmergesort code in jshow to create merge sort javacriptmerged sort javascript examplemerge sort an array jsmerge sort javascript toy problemquick sort javascriptbig of merge sort javascriptmerge sort javascript lomutosmerge arrays sorted javascriptmerge sort recursive javascriptmerge sort js examplemergesort in jsvsmergesort in jsmerge sort in js easymerge sort in place javascript3 way merge sort javascriptmerge sorts javascriptcombinesort in javascriptnode js merge and sort two unsorted arraysarray merge ordermerge sort javascript explanationhow to write merge sort algorithm javascriptconcat sort javascriptimplement merge sort using javascriptjs mergesortmerge sorted arrays javascriptmerge sorting in javascriptmerge sort implementation javascriptmerge sort technique javascriptmerge sort jvasciptjavascript merge sort algorithmmerge sorted array solution jsmerge and sort two arrays javascriptjavascript merge sormerge sort algorithm jsmerge sort javasciptmergesort algotirhm javascriptcombine sort javascriptjavascript merge sort array numbersmergesort implementation jsmerge sort javascriptmerge sort problem javascriptmerge sort code javascriptmerge sort enormous array javascriptjavscript merge sorhow to merge and sort 2 arrays in javascriptin place merge sort javascriptmarge sort javascriptmerge sort javasriptmerge sort javascript russmerge sort javascript examplemerge sort algorithm in javascriptmerge sort algorithm javascript codemerge sort methods jsmerge sort java scriptmerge sort in javascriptmerge sort javascript algorithmmerge sort javascfunction marge sort jsmerge sort algorithm jsmaking a merge sort javascriptmerge sort javascript with counterjavascript merge sortmerge sort javascript implementationmergesort javascript javascript merge sort array merge javascript sorthow is a merge sort algorithm implemented javascriptmerge sort javascriptgjavascript code for merger sortjs merge sortmerge sort algorithm in jsjavascript mergesortlearn merge sort javascriptjavascript array merge sortjs mergemerge sory javascriptmerge sort an array javascriptjavascript sort two arrays togethermerge sort javascript codehow to merge sort an array in javascriptmerge sort node jsjs temporary arrays for merge sortjoin and sort 2 arrays javascriptmerge arrays by order jspure javascript merge sort algorithmmerge sort js tutorialjavascript array sort using merge sortplain code javascript merge sort algorithmjavascript sample of merge sortarray sorting javascript mergesortmerge sorting javascriptmerge sort jsmergesort javascript solutionmerge sort js toy problemjs arrays merge and sortingmerge sort algorithm implementation javascriptjs shorthand array mergemarge sort jsjs in place merge sortmerge javascriptmerge sort jwhich array method uses mergesort in javascriptmerge sort function javascriptmerge sort solution javascriptsor and merge javascript arraymergesort jsmerge two sorted lists javascriptconcat sort jsjavascript merge sorted ararysmerge and sort two arrays jsmerge sort code in javascriptjs sort array of numbers merge sortmerge sort algorithm javascriptsplit 2c merge 2c mergesort javascriptjavascript mergesort 5dmerge sorted arrays in javascriptimplement a merge sort algorithm js