Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Merge Sort

Merge sort is based on Divide and conquer method. It takes the list to be sorted and divide it
in half to create two unsorted lists. The two unsorted lists are then sorted and merged to get a
sorted list. The two unsorted lists are sorted by continually calling the merge-sort algorithm; we
eventually get a list of size 1 which is already sorted. The two lists of size 1 are then merged.
Algorithm:
This is a divide and conquer algorithm. This works as follows
1. Divide the input which we have to sort into two parts in the middle. Call it the left part
and right part.
Example: Say the input is -10 32 45 -78 91 1 0 -16 then the left part will be -10 32 45 78 and the right part will be 91 1 0 6.
2. Sort each of them separately. Note that here sort does not mean to sort it using some other
method. We use the same function recursively.
3. Then merge the two sorted parts.
Input the total number of elements that are there in an array (number_of_elements). Input the
array (array[number_of_elements]). Then call the function MergeSort() to sort the input array.
MergeSort() function sorts the array in the range [left,right] i.e. from index left to index right
inclusive. Merge() function merges the two sorted parts. Sorted parts will be from [left, mid] and
[mid+1, right]. After merging output the sorted array.
MergeSort() function:
It takes the array, left-most and right-most index of the array to be sorted as arguments.
Middle index (mid) of the array is calculated as (left + right)/2. Check if (left<right) cause we
have to sort only when left<right because when left=right it is anyhow sorted. Sort the left part
by calling MergeSort() function again over the left part MergeSort(array,left,mid) and the right
part by recursive call of MergeSort function as MergeSort(array,mid + 1, right). Lastly merge the
two arrays using the Merge function.
Merge() function:
It takes the array, left-most , middle and right-most index of the array to be merged as
arguments. A temporary array (tempArray[right-left+1]) is required to store the new sorted part.
The current index position (pos) of the temporary array is initialized to 0. The left index position
(lpos) is initialized to left and right index position (rpos) is initialized to mid+1, of the array.
Until lpos < mid and rpos < right
if(array[lpos] < array[rpos]), i.e value of array at position lpos is less than value of

array at position rpos, then store array[lpos] (value at the left index of array) at current
index position (pos) of temporary array and increments the position index (pos) and left
position index (lpos) by 1. tempArray[pos++] = array[lpos++]
Else, store array[rpos] (value at the right index of array) at current index position (pos) of
temporary array and increments the position index (pos) and right position index (rpos)
by 1. tempArray[pos++] = array[rpos++]
Until (lpos <= mid) i.e. elements in the left part of the array are left
tempArray[pos++] = array[lpos++],store array[lpos] (value at the left index of array) at
current index position (pos) of temporary array and increments the position index (pos)
and left position index (lpos) by 1.
Until (rpos <= right) i.e. elements in the right part of the array are left
tempArray[pos++] = array[rpos++],store array[rpos] (value at the right index of array) at
current index position (pos) of temporary array and increments the position index (pos)
and right position index (rpos) by 1.
Finally copy back the sorted array to the original array.

Property:
1.
2.
3.
4.

Best case When the array is already sorted O(nlogn).


Worst case When the array is sorted in reverse order O(nlogn).
Average case O(nlogn).
Extra space is required, so space complexity is O(n) for arrays and O(logn) for linked
lists.

Example:
Unsorted Sequence
Number_of_elements
= 6, mid = 3, left = 0,
right = 5.
5 1 7 4 3 8
Recursive call to MergeSort function
Unsorted Sequence
Number_of_elements
= 3, mid = 1, left = 0,
right = 2.
5
1
7

Unsorted Sequence
Number_of_elements
= 3, mid = 1, left = 3,
right = 5.
4
3
8

Unsorted Sequence
Number_of_elements
= 2, mid = 3,3left = 3,
right = 4.
4
3
3 4

Unsorted Sequence
Unsorted Sequence
Unsorted Sequence
Number_of_elements
Number_of_elements
Number_of_elements
7
5
1
4
= 1,= mid
5, = 2, left = 2,
= 2, mid = 0, left
0, = 5, =left
1, =
mid
right = 1.right = 5.
right = 2.
8
5
1
7
1 5

3 4 8

1 5 7
Merge steps

Sorted Array

You might also like