In Progress
Lesson 18 of 43
In Progress

# Merge Sort

##### Akash

As another example of divide-and-conquer, we investigate a sorting algorithm that has the nice property that in the worst case its complexity is 0(n logn). This algorithm is called merge sort. We assume throughout that the elements are to be sorted in non decreasing order.Given a sequence of n elements( also called keys) a[l],… ,a[n], the general idea is to imagine them split into two sets a[l],… ,a[[n/2]] and a[[n/2]+ 1],… ,a[n]. Each set is individually sorted,and the resulting sorted sequences are merged to produce a single sorted sequence of n elements. Thus we have another ideal example of the divide-and-conquer strategy in which the splitting is into two equal-sized sets and the combining operation is the merging of two sorted sets into one.

MergeSort (Algorithm 6) describes this process very succinctly using recursion and a function Merge (Algorithm 7)which merges two sorted sets. Before executing Merge Sort,the n element should be placed in a[1 :n]. Then MergeSort (1,n) causes the keys to be rearranged into non decreasing order in a.

Below figure shows the diagram of working of merge sort algorithm in divide and conquer approach.