Algorithms 101: JavaScript Merge Sort and It’s Big O Notation
Merge Sort is an extremely popular sorting algorithm that combines merging and sorting exploiting the fact that empty or single element arrays are considered sorted. When scaled, this algorithm offers significant performance gains relative to less advanced sorting algorithms like Bubble Sort or Selection Sort. It is a member of the “divide and conquer” algorithm family that approach problem solving through deconstruction of a problem into smaller problems simple enough to solve directly with solutions that can then be collectively applied toward the original problem.
How It Works
Merge sort recursively splits an array into smaller empty or single element arrays that are then used to build back a sorted version of the original array.
So, how does that actually work?
Let’s say you have an unsorted array of seven elements:
Merge Sort first checks to see if the array has more than one element. If so, it divides the array at its midpoint.