merge sort javascript

Solutions on MaxInterview for merge sort javascript by the best coders in the world

showing results for - "merge sort javascript"
Alessio
14 Apr 2019
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}
Silas
26 Jul 2019
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}
Monica
22 Apr 2019
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])
Axelle
07 Nov 2019
1var array_length;
2/* to create MAX  array */  
3function heap_root(input, i) {
4    var left = 2 * i + 1;
5    var right = 2 * i + 2;
6    var max = i;
7
8    if (left < array_length && input[left] > input[max]) {
9        max = left;
10    }
11
12    if (right < array_length && input[right] > input[max])     {
13        max = right;
14    }
15
16    if (max != i) {
17        swap(input, i, max);
18        heap_root(input, max);
19    }
20}
21
22function swap(input, index_A, index_B) {
23    var temp = input[index_A];
24
25    input[index_A] = input[index_B];
26    input[index_B] = temp;
27}
28
29function heapSort(input) {
30    
31    array_length = input.length;
32
33    for (var i = Math.floor(array_length / 2); i >= 0; i -= 1)      {
34        heap_root(input, i);
35      }
36
37    for (i = input.length - 1; i > 0; i--) {
38        swap(input, 0, i);
39        array_length--;
40      
41      
42        heap_root(input, 0);
43    }
44}
45
46var arr = [3, 0, 2, 5, -1, 4, 1];
47heapSort(arr);
48console.log(arr);
49 
50
Tim
08 Apr 2019
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}
Paloma
17 Jan 2019
1/*  
2    a[] is the array, p is starting index, that is 0, 
3    and r is the last index of array. 
4*/
5
6#include <stdio.h>
7
8// lets take a[5] = {32, 45, 67, 2, 7} as the array to be sorted.
9
10// merge sort function
11void mergeSort(int a[], int p, int r)
12{
13    int q;
14    if(p < r)
15    {
16        q = (p + r) / 2;
17        mergeSort(a, p, q);
18        mergeSort(a, q+1, r);
19        merge(a, p, q, r);
20    }
21}
22
23// function to merge the subarrays
24void merge(int a[], int p, int q, int r)
25{
26    int b[5];   //same size of a[]
27    int i, j, k;
28    k = 0;
29    i = p;
30    j = q + 1;
31    while(i <= q && j <= r)
32    {
33        if(a[i] < a[j])
34        {
35            b[k++] = a[i++];    // same as b[k]=a[i]; k++; i++;
36        }
37        else
38        {
39            b[k++] = a[j++];
40        }
41    }
42  
43    while(i <= q)
44    {
45        b[k++] = a[i++];
46    }
47  
48    while(j <= r)
49    {
50        b[k++] = a[j++];
51    }
52  
53    for(i=r; i >= p; i--)
54    {
55        a[i] = b[--k];  // copying back the sorted list to a[]
56    } 
57}
58
59// function to print the array
60void printArray(int a[], int size)
61{
62    int i;
63    for (i=0; i < size; i++)
64    {
65        printf("%d ", a[i]);
66    }
67    printf("\n");
68}
69 
70int main()
71{
72    int arr[] = {32, 45, 67, 2, 7};
73    int len = sizeof(arr)/sizeof(arr[0]);
74 
75    printf("Given array: \n");
76    printArray(arr, len);
77    
78    // calling merge sort
79    mergeSort(arr, 0, len - 1);
80 
81    printf("\nSorted array: \n");
82    printArray(arr, len);
83    return 0;
84}
queries leading to this page
what is the time complexity of traversing an array using merge sort methodmerge sort injsmerge sort example stepsdifferent ways to do merge sortmerge sort javamerge sort uses which of the following method to implement sorting 3f 2amerge sort simple algmerge sort is in place sorting algorithmjavascript mergesort 5dcombinesort in javascriptarray sorting javascript mergesortmerge sort solution javascriptjavscript merge sorjs heap sortmearge sort cexplain merge sort with exampleheaps algorithm jsheapify javascriptmerge sorting algorithm basic operationsmerge sort algrithmis merge sort an in place algorithmmerge sort js toy problemmergesort 5cmerge sort in jsuse merge sort to sort in array with n elements what is the worst case time required for the fortmerge sort recursion merge functionhow long does the merge sort algorithm 27s runtimemerge sort algorithm elplementionmerge sort time complexityheap sort implementation javascriptmerge sort in c 2b 2bimplement merge sort merge sort algorithmsmerge sort function javascriptjavascript merge sort algorithmmergesort 28 29 22algorithm 22 for merge sortjava merge sort algomergesort algorithmusmerge sort uses which technique to implement sortingexplain merge sort algorithmmerge sort javascript examplenode js merge sortmerge sort algorithm poudowrite an algorithm for merge sortmerge sort javascripthow to implement merge sort javascriptmerge sort mergingmerge sort algorithmerge sort technique javascriptmerge sort recursion c 2b 2bmerge sort programizarray sort js merge sortgfg merge sortquick sort javascriptmerge sort matching algorithmmerge sort algorithm c implimentationalgoritmi merge sorthow does merge sort workmerge sort an array javascriptprogram for merge sortimplement merge sort javascriptmerge sort simple algorithmgeeks for geeks merge sortjavascript heapifymerge sort explainedmerge sort examplemerge sort definationpseudo code for merge sortmerge sort usesmerge sort algorithms explainedmerge sorts javascripthow long does the merge sort algorithm runtimemerge algorithm javascriptheap sort javascriptheapsort in jsmergesortmerge sort algorithm pseudocodeapproach to do merge sortmerge sort gfgmerge function for merge sortwhat is merge sort in algorithmmerge sort js 27what is a merge sorthow does a mergey sort algorithm workwrite algorithm for merge sort method merge procedure of merge sortmerge sort time and space complexitymergesort javascriptmerge sort code javascriptwhat is the basic operation in merge sortmerge sort an array jscode merge sortwhy do we call mid 2b1 in merge functioneasy merge sort explanationconcat sort javascriptmerge sorting algorithmmerge sort in javasriptalgorithms mergesortmerge sort uses which of the following algorithm to implement sorting 3fmerge sorting in javascriptmergesort in jsvsheap implementation in javascriptheap in javascriptmergesort algotirhm javascriptmergesort em jswhat is merge sort algorithmmerge sort stepsmerge sort codemerge sort algotithmmerge sort quorajavascript merge sort array numberssort concat jsmerge algorith merge sortmerge sort in place algorithmwrite an algorithm for merge sort with an given example merge sort 3ajavascript merge sortnode js merge and sort two unsorted arraysmerge sorted arrays jswhere to use merge sortmerge sort explained javascriptwhich of the following sorting algorithm makes use of merge sort 3fmerge sort orderexplain merge sort step by step algorithmmerge sort javascript codemerge in merge sortcombine sort javascriptmergesort implementation c 2b 2bmerge sort code exampleheapify algorithm javascriptmerge arrays by order jsmerge sort alsopure javascript merge sort algorithmmerge sort with javascriptimplement merge sort using javascripttwo way merge sortmerge sort javascript merge sort algorithm of merge sortin place sorting algorithm merge sortmerge sort algoritmomerge while sorting two arrays recursivelyheapsort javascrriptmerge sort js looppseudocode merge sortmerge sort algorithm example step by stepmerge sort algo gfgmerge sort c 2b 2bmerge sort algorithm javascript codemerge sort for arrayhow to do heap sort in javascriptcomplexity of 3 way merge sortarray merge orderc 2b 2b merge sort functionjs heap sort codemerge sort algorithm in javascriptjs mergesortmergesort javasciirptbriefly explain merge sort techniquemerge sort algorithm example in javascriptmerged sort javascript examplemerge sort algorithm purposemerge sort uses an algorithmic technique known as merge sort in javascriptimplement the merge step of the merge sort algorithmexamples of algorithms merge sortmerge sort javascriptmergesort implementation jsmerge in merge sort explainedinsertion sort algorithmmergesort i 2c jmerge sort in greekin place sorting algorithm merge sortmerge short javascriptmerge sort code in javascriptwhat is merge sortingexplain the working of merge sort 3fmerge array sorted javascriptwhen do we use merge sortmerge sort divide and conquerjavascript merge sort in one functionhow created the merge sort algorithmmaking merge sort javascriptc 2b 2b merge sort arraymerge sort algorithm and analysismerge sort t 28n 29 expressionmerge sort for sorted arraypurpose of merge sortmerge sort algorithm in jsmerge sort algorithm onlinemarge sort jsmerge sory javascriptis merge sort the best sorting algorithmwhat is merge 27 sortwhich of the following algorithm design technique is used in merge sortmerge sort complexity analysismerge sort geeks for geekstwo way merge sort algorithmmerge sort algorithm stepswhat is the use of merge sorthow to sort an array of strings using merge sort javascriptsplit 2c merge 2c mergesort javascriptdivide and conquer sorting algorithm code in c 2b 2bmerge sort algorithmmerge sorted arrays javascriptdescribe the concept of merge sortmerge sort algorithm 5ctechnique used in merge sortmerge and sort algorithmsort array merg sort in cmergesort jshow to write a program to implement merge sort 3fjs mergeis merge sort in place sorting algorithm merge sort algorithm representationmerge sorting using javascriptheap sort in javascript apply merge sort to sort the listjavascript heapsortjavascript merge sorted ararysmarge sort javascriptmerge sort analysis of algorithmsmerge sort implementation javascriptwhich of the following algorithm design technique is used in merge sort 3fmerge sorted arrays in javascriptmerge sort implementationmerge sort alogrythmheapsort in javascriptmerge sort javascript with counterpseudocode for merge sortmerge sort implementation exampleexample of merge sortmerge sort algorithm explainedheap 27s algorithm javascriptmerge sort javascript implementationin place merge sort javascriptwho invented merge sortimplement heap sort using jsjs in place merge sortimplement a merge sort algorithm jsmerge sort programlearn merge sort javascriptmerge sort in placealgoritma merge sortmerge sort js tutorialrecursive merge sort where we use the index and not the arrayrecurrence relation of merges ortjavascript heap sorttechnique of merge sortmerge sort algorithm in javamerge sort algorythmmerge sort javasciptjs sort array of numbers merge sortwhat is merge sortmerge sort javascirptmerge sort algorithm in clr book explanationmerge sort of arraymerge sort algorithm is in place 3fjavascript heap ordermerge sort algorithm with exampleheap sort jsmerge sort node jspmergew saoprt jsarray merge javascript sortmerge sort algorithm exampleheap sort algorithm javascriptmerge sort searchmerge sort divides the list inmerge sort algoritm explainedheapsort jsheap sort algo javascriptmerger sortcan you explain how merge sort workmerge sort algorothmmerge sort algorithmsmerge sort uses which of the following method to implement sorting 3fmergesort merge functionmerge sort javascriptmerge sort program in javascriptsimple merge sortalgo of merge sortwrite the algorithm for merge sortmerge sort algomerge javascriptmerge two sorted lists javascriptis merge sort an in place algorithmwrite a program for merge sortfastest merge sort jsanalysis of merge sort algorithmmerge sort enormous array javascriptheap javascripthow is a merge sort algorithm implemented javascriptjavascript array sort using merge sorto 281 29 algorithm merge sortbest merge sort algorithmjavascript heapsort libraryarray sortjs mergesortmerge sort explanationmergesort javascriptmerge sort questionheap sort jaavscriptmerge sort for sorted arraysimplement a merge sort javascript merge sormerge sort algormerge sort sorts in placemerge sort algorithm jsmerge sort pseduodmerge sort jmerge arrays sorted javascriptmerge sort javascript explanationjs heapsortheap sort javascriptmerge sort methods jsis merge sort a in place algorithmsort the array using merge sortmerge sort step by step algorithmmerging in data structuremerging algorithm in mergesortwhich of the following method is used for sorting in merge sort algorithm 3fstep by step algorithm for merge sortmerge algorithm in merge sorthow to identify merge sort algorithm workshow to create merge sort javacriptimplementation of merge sort algorithm in c 2b 2bmerge sort uses which of the following algorithm to implement sortingheap sort nodegiven an array of n elements 2c that is reverse sorted the complexity of merge sort to sort it ismerge sort in place javascriptmerge sorted array solution jsjavascript merge sorted arraysheap sorting array javascriptmerge sort algrotj 2csmerge sort logic in jsmerge sort uses which of the following techniques to implement sortingmerge sorted arraymerge sort uses which programming approachmerge sorted array javascriptmerge sort alogorithmmerge sort algorithm step by stepheaps in javascriptwhich best describes a merge sort algorithm 3fjavascript array merge sortalgorithm for merge sortmerge sort pseudocodemerge sort in c using recursion pointersjavascript mergesort3 way merge sort javascriptmerge sort uses which of the following technique to implement sorting 3f 2amerge sort techniqueheaps javascriptmerge sort divide list into n 2f4 and3n 2f4merge sort recursive javascriptmerge sortingmergesort in javascriptspecial about merge sortmerge sort js examplemerge sort algorithm jsmerge sort function in algorithm hmerge sort uses which of the following technique to implement sortingwhere we use merge sortmergesort in jsalgorithms merge sort 29 write a program to implement merge sort merge sort algorithm javascriptmerge sort algorihtmassume that a merge sort algorithmsort merge algorithmmerge sort inconcat sort jsmerge and merge sortheap sort algorithm in javascriptmerge and sort two arrays jsdesign merge sort algorithmhow the merge sort algorithm worksmerge sortwhat is merge sort algorithm with example 3fmerge sort explaindemerge sort algorithm programizmerge searchhow to merge sort in jsmerge algorithmmerge algorithm in the merge sort algorithmheap sorting javascriptmerge sort java scriptwhich algorithm technique merge sort usessorting programiz merge sort algorithmmerge sort nlognheap sort in jsmerge sort array javascriptmerge sort geeksforgeeksjs merge sort functionmerge sort code jsmerge sorting merge sorting javascriptalgorithm of a merge sortmergesort algorithm2 way merge sort algorithmmaking a merge sort javascriptmerge sort javascriptgplain code javascript merge sort algorithmmerge sort applicable for repeating elementsmerge sort javasriptmerge sort use which of the following technique to implement sortingsteps of merge sort algorithmwhich array method uses mergesort in javascriptimplementing the merge sort algorithmmerge sort algorithm jennyheap data structure javascriptlist steps that the merge sort algorithm would make in sorting the following values 4 2c1 2c3 2c2merge sort code for c 2b 2bwhat does a merge sort domergesort algorithm explainedmerge array sortimplementing merge sortwrite a program to implement merge sort which of the following algorithm design techniques is used in merge sort 3fjavascript heap data structure 2cerge sort jsmerge sort algorithm coding explanationmerge sort method algorithmmerge sort jvasciptmerge sort uses which of the following technique to implement sorting 3fmergesort gfg3way merge sorttop down merge c languagemerge sort in javvascripitmerge sort jsis the merge sort an in place algorithm 3fsorting merge sort in jsis merge sort in place algorithmin place sorting merge sortmerge steps of merge sort algorithmmerge sort site 3arosettacode orgmerge sort jsmerge sort algorthmsort heap javasciptdiscuss merge sort algorithm with an example merge step in merge sorthow merge sort worksmerge sort in js easythe merge sort algorithm is an example for 3ajs merge sorthow merge sort algorithm workshow does a merge sort algorithm workexplain merge sort algorithm with examplemerge sorting exampletotal no of operations in merge sortheapsort javascriptmerge sortmerge sorting algorithmsheap sort nodejsmerge sort for denominationsmerge sort algorythemmerge sort algorithm worksheap javascript implementationmergesort code in jsmerge sort faboconimerge sort works best formerge sort is an in place sorting algorithmmerge sort for sorting numbersjavascript sample of merge sortmerge sort problem javascriptprogram to implement merge sortsort heap on array javascriptjs concat and sortmerge sort algorithm 2cwrite the merge sort algorithm steps merge sort javascript