0% found this document useful (0 votes)
28 views

Merge Sort

Merge Sort
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Merge Sort

Merge Sort
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

DATASTRUCTURE & ALGORITHMS

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 uses a divide and conquer paradigm for sorting.

*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.

* Then, we increment i and k by 1.


Then, we have
Step-03:
* We have i = 1, j = 0, k = 1.
* Since L[1] > R[0], so we perform A[1] = R[0] i.e. we copy the first element from right
sub array to our sorted output array.

* Then, we increment j and k by 1.


Then, we have
Step-04:
* We have i = 1, j = 1, k = 2.
* Since L[1] > R[1], so we perform A[2] = R[1].
* Then, we increment j and k by 1.
Then, we have
Step-05:
*We have i = 1, j = 2, k = 3.
*Since L[1] < R[2], so we perform A[3] = L[1].
*Then, we increment i and k by 1.
Then, we have
Step-06:
*We have i = 2, j = 2, k = 4.
*Since L[2] > R[2], so we perform A[4] = R[2].
*Then, we increment j and k by 1.
Then, we have
Step-07:
* Clearly, all the elements from right sub array have been added to the 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.

Finally, our sorted output array is


Merge Sort Algorithm
* Merge Sort Algorithm works in the following steps-
* It divides the given unsorted array into two halves- left and right sub arrays.
* The sub arrays are divided recursively.
* This division continues until the size of each sub array becomes 1.
* After each sub array contains only a single element, each sub array is sorted
trivially.

* Then, the above discussed merge procedure is called.


* The merge procedure combines these trivially sorted arrays to produce a final
sorted array.

You might also like