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

2.

Merge Sort
Merge sort is a divide-and-conquer algorithm based on the idea of breaking
down a list into several sub-lists until each sublist consists of a single element
and merging those sublists in a manner that results into a sorted list.
It divides the input array into two halves, calls itself for the two halves, and then
merges the two sorted halves.
In mergesort, we will have 2 functions:
mergesort(low,high)
merge(low,mid,high) // The merge() function is used for merging two
halves.

In merge sort we follow the following steps:

1. We take a variable p and store the starting index of our array in this. And we
take another variable r and store the last index of array in it.
2. Then we find the middle of the array using the formula (p + r)/2 and mark the
middle index as q, and break the array into two subarrays, from p to q and from
q + 1 to r index.
3. Then we divide these 2 subarrays again, just like we divided our main array
and this continues.
4. Once we have divided the main array into subarrays with single elements, then
we start merging the subarrays.

* MergeSort algorithm
* Merge Algorithm

Time Complexity:
Merge Sort is a recursive algorithm and time complexity can be expressed as
following recurrence relation.
T(n) = 2T(n/2) + θ(n)

The above recurrence can be solved either using the Master method. It falls in case II
of Master Method and the solution of the recurrence is θ(nLogn). Time complexity of
Merge Sort is θ(nLogn) in all 3 cases (worst, average and best) as merge sort always
divides the array into two halves and takes linear time to merge two halves.
Example:

Merge sort on an array of 9 elements


3. Strassen’s Matrix Multiplication

Let us consider two matrices X and Y. The goal is to calculate the resultant matrix Z
by multiplying X and Y.

Matrix Multiplication using Divide and Conquer


Let us take two matrices A and B of the order 2x2 and let C be the resultant matrix of
the multiplication of A and B.
Matrix_mult(a,b,n)
{
if(n<=2)
{
C110 = A110 .B110 + A120 .B21
C120 = A110 .B120 + A120 .B22
C210 = A210 .B110 + A220 .B21
C110 = A210 .B120 + A220 .B22
}
else
{
mid = n/2
Matrix_mult(A11,B11,n/2)+0 Matrix_mult(A120 ,B210 ,n/2)
Matrix_mult(A11,B12,n/2)+0 Matrix_mult(A120 ,B220 ,n/2)
Matrix_mult(A21,B11,n/2)+0 Matrix_mult(A220 ,B210 ,n/2)
Matrix_mult(A21,B12,n/2)+0 Matrix_mult(A220 ,B220 ,n/2)
}
}
* Matrix Multiplication using Divide and Conquer

For multiplying two matrices of size n x n, we make 8 recursive calls above, each
on a matrix/subproblem with size n/2 x n/2. Each of these recursive calls
multiplies two n/2 x n/2 matrices, which are then added together. For the
addition, we add two matrices of size n20 /4, so each addition takes Θ(n20 /4)
time. We can write this recurrence in the form of the following equations:

Using Master’s theorem, we get the time complexity as big O(n30 ).


Here, we calculate the resultant matrix C by using the formulae (P1 to P7) given
below:

Time Complexity:

You might also like