Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views19 pages

Merge Sort M Tech

The document provides an overview of the Merge Sort algorithm, detailing its divide-and-conquer approach which includes dividing the problem, recursively solving subproblems, and combining solutions. It explains the key operation of merging sorted sequences and analyzes the algorithm's time complexity, concluding that the worst-case running time is θ(n log n). The document also mentions the use of recurrence equations to describe the running time of divide-and-conquer algorithms.

Uploaded by

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

Merge Sort M Tech

The document provides an overview of the Merge Sort algorithm, detailing its divide-and-conquer approach which includes dividing the problem, recursively solving subproblems, and combining solutions. It explains the key operation of merging sorted sequences and analyzes the algorithm's time complexity, concluding that the worst-case running time is θ(n log n). The document also mentions the use of recurrence equations to describe the running time of divide-and-conquer algorithms.

Uploaded by

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

Advanced Algorithms (MTCS 102)

UNIT-1
Merge Sort Algorithm

From:
Asif Khan
Department of CSE
GNIOT, Greater Noida

Reference:
Thomas H. Corman et al.
The MIT Press
Merge Sort
The divide-and-conquer approach:
Divide the problem into a number of subproblems that are
smaller instances of the same problem.

Conquer the subproblems by solving them recursively. If


the subproblem sizes are small enough, however, just
solve the subproblems in a straightforward manner.

Combine the solutions to the subproblems into the


solution for the original problem.
Merge Sort
The merge sort algorithm closely follows the divide-and-
conquer paradigm. Intuitively, it operates as follows.
Divide: Divide the n-element sequence to be sorted into
two subsequences of n/2 elements each.

Conquer: Sort the two subsequences recursively using


merge sort.

Combine: Merge the two sorted subsequences to produce


the sorted answer.
Merge Sort
Merge Sort
The key operation of the merge sort algorithm is the
merging of two sorted sequences in the “combine” step.
We merge by calling an auxiliary procedure
MERGE(A,p,q,r),
where A is an array and p, q, and r are indices into the
array such that p < =q < r.
The procedure assumes that the subarrays A(p .. q) and
A(q+1 .. r) are in sorted order. It merges them to form a
single sorted subarray that replaces the current subarray
A(p .. r).
Merge Sort
Merge Sort
Merge Sort
Merge Sort
Merge Sort
Merge Sort

Cont...
Merge Sort
Analyzing Divide-and-Conquer Algorithms
When an algorithm contains a recursive call to itself, we
can often describe its running time by a recurrence
equation or recurrence, which describes the overall
running time on a problem of size n in terms of the
running time on smaller inputs. We can then use
mathematical tools to solve the recurrence and provide
bounds on the performance of the algorithm.

A recurrence for the running time of a divide-and-conquer


algorithm falls out from the three steps of the basic
paradigm. As before, we let T(n) be the running time on a
problem of size n.
Analyzing Divide-and-Conquer Algorithms
If the problem size is small enough, say n <= c for some
constant c, the straightforward solution takes constant
time, which we write as θ(1). Suppose that our division of
the problem yields a subproblems, each of which is 1/b
the size of the original. (For merge sort, both a and b are
2, but we shall see many divide-and-conquer algorithms in
which a ≠ b.) It takes time T(n/b) to solve one subproblem
of size n/b, and so it takes time aT(n/b) to solve a of
them. If we take D(n) time to divide the problem into
subproblems and C(n) time to combine the solutions to
the subproblems into the solution to the original problem,
we get the recurrence :
Analyzing Divide-and-Conquer Algorithms
Analysis of Merge Sort
Time Complexity of MERGE: The MERGE procedure
runs in θ(n) time, where n =r-p+1, observe that each of
lines 1–3 and 8–11 takes constant time, the for loops of
lines 4–7 take θ( n1+n2) = θ(n) time, and there are n
iterations of the for loop of lines 12–17, each of which
takes constant time.
Time Complexity of MERGE-SORT:
Although the pseudo code for MERGE-SORT works
correctly when the number of elements is not even, our
recurrence -based analysis is simplified if we assume that
the original problem size is a power of 2. Each divide step
then yields two subsequences of size exactly n/2.
Analysis of Merge Sort
We will set up the recurrence for T(n), the worst-case
running time of merge sort on n numbers. Merge sort on
just one element takes constant time. When we have n > 1
elements, we break down the running time as follows.
Divide: The divide step just computes the middle of the
subarray, which takes constant time. Thus, D(n) = θ(1).
Conquer: We recursively solve two subproblems, each of
size n=2, which contributes 2T(n/2) to the running time.
Combine: We have already noted that the MERGE
procedure on an n-element subarray takes time θ(n) and so
C(n) = θ(n).
Analysis of Merge Sort
The recurrence for the worst-case running time T(n) of
merge sort:

In next classes, we will study the “master theorem,” which


we can use to show that T(n) is θ(n lg n).
THANK YOU

You might also like