Using Merge Sort

Although the quicksort on average is pretty fast, it still has the theoretical worst time complexity of O(n²). In this section, we shall examine another sorting algorithm, called merge sort, in which the worst time complexity is O(n log n). Similar to quick sort, merge sort belongs to the divide and conquer class of algorithms.

Merge sort can be summarized in three simple steps as follows: 

  1. Split the array in the middle
  2. Recursively sort each part separately
  3. Merge the two sorted parts together

In the following section, we will develop the preceding steps gradually, at each turn slowly building our understanding of how merge sorting works.

Although merge sort is theoretically faster than quick sort, in practice, some implementations of quick sort can be more efficient than merge sort. Additionally, the merge sort uses about O(n) memory as opposed to quick sort, which is O(log n).
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset