Merge Sort
Merge Sort
MERGE SORT
UNIT-3
S.Kavitha
Head & Assistant Professor
Department of Computer Science
Sri Sarada Niketan College of Science for
Women,Karur.
Merge Sort
*Merge sort is a famous sorting algorithm.
*It divides the problem into sub problems and solves them
individually.
*It then combines the results of sub problems to get the solution of
the original problem.
How Merge Sort Works?
* Before learning how merge sort works, let us learn about the merge procedure of
merge sort algorithm.
* The merge procedure of merge sort algorithm is used to merge two sorted arrays
into a third array in sorted order.
* Consider we want to merge the following two sorted sub arrays into a third array
in sorted order
The above merge procedure of merge sort algorithm is
explained in the following steps
Step-01:
* Create two variables i and j for left and right sub arrays.
* Create variable k for sorted output array.
Step-02:
* We have i = 0, j = 0, k = 0.
* Since L[0] < R[0], so we perform A[0] = L[0] i.e. we copy the first element from left
sub array to our sorted output array.
* So, we exit the first while loop with the condition while(i<nL && j<nR) since now
j>nR.
* Then, we add remaining elements from the left sub array to the sorted output array
using next while loop.