Merge Sort:
Print this page
   
  • The time complexity of the sorting algorithms discussed till now are in O(). That is, the number of comparisons     performed by these algorithms are bound above by c, for some constant c > 1.
  • Can we have better sorting algorithms? Yes, merge sort method sorts given a set of number in O(n log n) time.
  • Before discussing merge sort, we need to understand what is the meaning of merging?

Merge sort Method :

Definition:
Given two sorted arrays, a[p] and b[q], create one sorted array of size p + q of the elements of a[p] and a[q] .

  • Assume that we have to keep p+q sorted numbers in an array c[p+q].
  • One way of doing this is by copying elements of a[p] and b[q] into c[p+q] and sort c[p+q]which has time complexity of atleast O(n log n).
  • Another efficient method is by merging which is of time complexity of O(n) .
  • Method is simple, take one number from each array a and b, place the smallest element in the array c. Take      the next elements and repeat the procedure till all p+q element are placed in the array c.
                                                                                                                An Applet Demonstration of Merging
 
Prev