JavaScript Algorithms and Data Structures: Sorting - Merge Sort

Merge sort is a sorting algorithm that works by recursively splitting the array into smaller subarrays and then merging the sorted subarrays back together. Learn how to implement merge sort in JavaScript.

In computer science, merge sort (also commonly spelled mergesort) is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Mergesort is a divide and conquer algorithm that was invented by John von Neumann in 1945.

An example of merge sort. First divide the list into the smallest unit (1 element), then compare each element with the adjacent list to sort and merge the two adjacent lists. Finally all the elements are sorted and merged.

Merge Sort

A recursive merge sort algorithm used to sort an array of 7 integer values. These are the steps a human would take to emulate merge sort (top-down).

Merge Sort

Complexity

NameBestAverageWorstMemoryStableComments
Merge sortn log(n)n log(n)n log(n)nYes 

References

The Original Article can be found on https://github.com

#javascript #algorithms #datastructures #sorting

JavaScript Algorithms and Data Structures: Sorting - Merge Sort
2.90 GEEK