The merge sort

The merge sort algorithm is the first sorting algorithm that can be used in a real-world scenario. The three first sorting algorithms that we learned about in this book do not have a good performance, but the merge sort has good performance with a complexity of O(n log n).

The JavaScript Array class defines a sort function (Array.prototype.sort) that can be used to sort arrays using JavaScript (with no need to implement the algorithm ourselves). ECMAScript does not define which sorting algorithm needs to be used, so each browser can implement its own algorithm. For example, Mozilla Firefox uses the merge sort as the Array.prototype.sort implementation, while Chrome (V8 engine) uses a variation of the quick sort (which we will ...

Get Learning JavaScript Data Structures and Algorithms - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.