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

Merge Sort

Uploaded by

Mithun
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 views

Merge Sort

Uploaded by

Mithun
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/ 10

CHAPTER

5
Merge Sort

5.1 Introduction

This algorithm was invented by John von Neumann in 1945. It closely follows the
divide-and-conquer paradigm.
Conceptually, it works as follows:
1. Divide. Divide the unsorted list into two sub lists of about half the size
2. Conquer. Sort each of the two sub lists recursively until we have list sizes of length
1, in which case the list itself is returned
3. Combine. Merge the two-sorted sub lists back into one sorted list.

We note that the recursion "bottoms out". When the sequence to be sorted has length 1, in
Which case there is no work to be done, and since every sequence of length 1is already in sorted
order. The key operation of the merge sort algorithm is the merging of two sorted sequences in the
"combine" step. To perform the merging, we use an auxiliary procedure MERGE (A, P, q, r), where
AIS an array and p, q, and r are indices numbering elements of the array such that psq<r. The
Procedure assumes that the sub arrays A[p..ql and A<q+ 1..r]are in sorted order. It merges them to
torm a single sorted sub array that replaces the current sub array A[p..r].
95
96 ALGORITHMS DESIGN AND
ANALYSIS
MERGE (A, p, q, r)
1. n, - q -p+1
2. n, - r - q
3. create arrays L[1..n, +1]and R[1.. n, + 1]
4. for i 4- 1 to n,
5. do L[i] ¬- A(p+i-1]
6. for j -1 to n,
7. do RU] A
[q +j]
8. L(n, +1]o
9. R(n, + 1] o
10. i+1
11. j+1
12. for k 4p to r
13. do if L[]sRU]
14. then A[k] Li]
15. i+i+1
16. else A[k]RÚ]
17. j+j+1

It is easy to imagine a MERGE procedure that takes time 0(n) where n=r-p+1 is the
number of elements being merged.
We can now use the MERGE procedure as a subroutine in the merge sort algorithm. The
procedure MERGE-SORT (A p.r) sorts the elements in the sub array A[p..1) If ps, the sub array
has at most one element and is therefore already sorted. Otherwise, the divide step simply
computes an index q that partitions A[p..r] into two sub arrays: A(p..g), containing [n/2]
elements, and A[q+1..r], containing |n/2 Jelements.
MERGE-SORT (A, p, )
1. if p <r
2. then q -(2+)/2|
3. MERGE-SORT (A,p, q)
4. MERGE-SORT (A, q+1,r)
MERGE (A,p, q, r)

To sort the entire sequence A=(4[1), A(2],.. A[n]) we callMERGE-SORT (A, 1, length[ A)
where once again length [A] =n If we look at the operation of the procedure bottom-up when nis a
power of two, the algorithm consists of merging pairs of 1-item sequences to form sorted
sequences of length 2, merging pairs of sequences of length 2to form sorted sequences of length
and so on, until two sequences of length n/2are merged to form the final sorted sequence of lens
n. Figure shows this process in the array (5,2,4,6,1,3,2,6).
MERGE SORT
97

Sorted sequence
1 2 2 3 4 5 6 6

Merge
2 4 5
12 3 6

Merge Merge
2 5 4 6 1 3 2 6

Merge Merge Merge Merge


5 2 4 6 1 3 2 6

Initial sequence

Figure 5.1

Example of Merge Sort


0 MS 85 24 63 45 17 31 96 50

1Div 85 24 63 45 17 31 96 56

1MS 85 24 63 45

2 Diy 85 24 63 45

2MS 85 24

3 Div 85 24

3MS 85

3 MS 24

3 Merge 24 85

2MS 63 45

3 Div 63 45

3 MS 63

3 MS 45

3Merge 45 63

2 Merge 24 45 63 85
98 ALGORITHMS DESIGN AND ANALYSIS

17 31 96 50
24 63 45
0 MS 85
17 31 96 50
85 24 63 45
1Div
24 63 45
1MS 85

45 63 85
2 Merge 24
17 31 96 50
1MS
17 31 96 50
2 Div
17 31
2MS
17 31
3 Div
17
3 MS
31
3 MS
17 31
3 Merge
96 50
2 MS
96 50
3 Div
96
3 MS
50
3 MS
50 96
3 Merge
17 31 50 96
2 Merge
45 50 63 85 96
17 24 31
1 Merge

20, 25, 30, 40, 35.


Example. Using merge-sort algorithm to sort the following elements : 15, 10, 5, [B.Tech UPTU09-10]

Solution. Suppose A[]


3 4 5 6 7 8
1
15 10 5 20 25 30 40 35

Here p=1; r=8

1<8 then q=l(1+8)/2 |=|4.5]=4


i.e., q=4
MERGE SORT
99

call MERGE-SORT (A, 1,4)


MERGE-SORT (A, 5, 8)
MERGE (A, 1, 4, 8)
First call MERGE-SORT (A, 1, 4)
Here p=1
r=4

1<4 so 4--25|-2
9=2

then call MERGE-SORT (A, 1, 2)


MERGE-SORT (A, 3, 4)
MERGE (A, 1, 2, 4)
Now call MERGE-SORT A, 1, 2) firstly

Here p=1
r=2

1<2 then

So, call MERGE-SORT (A, 1, 1)


MERGE-SORT (A, 2, 2)
MERGE-SORT (A, 1, 1, 2)
call MERGE-SORT (A, 1, 1)
Here 1 1No change.
then call MERGE-SORT (A, 2, 2)
Here 2 1so, no change.
then call MERGE (A, 1, 1, 2)
Here p=1, q=1, r=2
SO, 14 =9-p+1 =1 -1+1

n, =2-1
n, =1
100 ALGORITHMS DESIGN AND ANALYSIS

Create arrays L [1..2]and R[1...2]


for L=1to 1
L{1] = A[1+1-1] A] 15 10

L[1]= A[1]
1
15
for j=1 to 1 L]
R[1] = A[1+1]
1
R[1] = A(2] 10
R[]
and i=1
Now L[2]=0
R(2]=0 j=1
For k =1 to 2
k=1
if L[1]< R[1]
15 <10 (false)
So A[1]= R[1] i.e., A[1]=10
and j=1+1=2.
Now k=2 and j=2.
L[1]< R[2]
15 <o (true)
So i=1+1=2 and A[2]=15

Now A[]=
1 2
10 15

Now MERGE-SORT (A, 1, 2) is completed

then call MERGE-SORT (A, 3, 4)

Here p=3
r=4

3<4 then

call MERGE-SORT (A, 3, 3) (No change)


MERGE-SORT (A, 4, 4) (No change)
MERGE (A, 3,3, 4)
MERGE SORT 101

So, call MERGE (A, 3, 3, 4)


Here p=3, q=3, t=4.
So, =1

So, create array L and R.


for i= 1 to 1

L[1]=A[3+1-1]
L[1]= A[3] i.e., L[1]=5
for j =1tol
R[1] = A(3+1]
i.e., R[1] = 20.
L[2]=0 and R[2]=0
and i=1 and j=1.
Now fork=3 to 4
k=3 i=1 j=1
L[1]=5
R(1]=20
Here 5 S20 (True)
then A[3]=5 and i1+1=2

k=4 i=2 j=1


LÊ2]=o R[1] =20
I2]k R[1], so,
A[4]=20
i.e., Now
1 2 3 4
10 15 5 20

Now cal MERGE (A, 1, 2, 4)


Here p=1 q=2 r=4
n =2-1+1
n =2 i.e., n, =2
n, =4-2 =2 n, =2.
Create array L[1.3] and R[1.3]
For i=1 to 2
i=1 L1]= A[1+1-1]
L[1]= A[1] i.e., L[1]=10
102 ALGORITHMS DESIGN AND

i=2 L [2]= A[1 +2-1]


ANALYSIs
- A[2] 2 3
LÊ2]= 15 i.e., LU 10 15

For j =1 to 2
R{1]= A[2 +1] =5 1 2 3
R(2] = A[2 +2] = 20 i.e., R[] 5 20

Now i=1; j=1


For k= 1to 4
k=1 L[1] s R[1] i.e., 10 s5(False)
SO A[1]= R[] i.e., A[1]=5 and j=1+1=2

k=2 L[1] s R[2] i.e., 10 <20 (True)


so A[2]= L[1] i.e., A[2]= 10 and i=1+1=2

k=3 L[2] < R2] i.e., 15 <20 (True)


so A(3]= L[2] i.e., A[3]=15 and i=2+1=3

k=4 L{3] < R[2] i.e., 0S 20 (False)


so A[4]= R[2] i.e., A[4]=20 and j=2+1=3

ie., now array is


1 2 3 4
A[) 5 10 15 20

Now call MERGE-SORT (A, 5, 8)

Here p=5
r=8

5 <8 sO
i.e., q=6
then call MERGE-SORT (A, 5, 6)
MERGE-SORT (A, 7, 8)
MERGE (A, 5, 6, 8)
MERGE SORT 103

First we call MERGE-SORT (A, 5, 6)


Here p=5 r=6
5+6
5<6 then q==5
2

then call MERGE-SORT (A, 5


MERGE-SORT (A, 6, 6)
MERGE (A, 5, 5, 6)

call MERGE-SORT (A,5, 5)


Here 5 < 5(False) so, No change
then MERGE-SORT (A, 6, 6)
Here 6 < 6 (False) so, No change
call MERGE (A, 5, 5, 6)
Here p=5 q=5 r=6

n, =1
Create arrays L[1.2] and R[1.2]
L[1] = A[5] i.e., L[1]=25
R[1]= A[6] 1.e., R[1] =30
1 2
LÊ2]=o i.e., L[] 25

1 2
R(2]=0 30
i.e., R[]
i=1 and j=1
For k =5 to 6

k=5 L[1] s R[1] i.e., 25s30(True)


SO A[5] =25 and i=1+1=2

k=6 L[2] sR[1] o.s 30(False)


i.e.,
SO A[6]=30 and j=1+1=2

Thus now array is


4 5 6
1 2
15 20 25 30
A| 5 10
ALGORITHMS DESIGN AND
104 ANALYSIS
Now call MERGE-SORT (A, 7, 8)
We get 5 6 7
3 4
1 2
20 25 30 35 40
A|) 5 10 15

Now call MERGE (A, 5, 6, 8)


We get the array
3 4 5 6 7 8
1 2

5 10 15 20 25 30 35 40

Now call MERGE (A, 1,4, 8)


We get the sorted array as
2 3 4 5 6 7 8
1

5 10 15 20 25 30 35 40

This is fnal sorted array.

5.2 Analysis of Merge Sort


Although the pseudocode 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 apower of two. Each divide step then yields two subsequence of size exactly n/2. This
assumption does not affect the order of growth of the solution to the recurrence.
Merge sort on just one elenment takes constant time. When we have n >1elements, we break
down the running time as follows.
Divide. The divide step just computes the middle of the sub array, which takes
constant time. Thus, D(n) =0(1)
Conquer. We recursively solve two sub problems, 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 sub
array takes time 9(n) so C(1) =(n)
When we add the functions D(n) and C(n) for the merge sort analysis, we are adding a
function that is 9(n) and a function that is 9(1) This sum is a linear function of n, that is, 0(1)
Adding it to the 2T (n/2) term from the "conquer" step gives the recurrence for the worst-case
running time T(n) of merge sort:
T(m)=O(4) if n=1
2T(n/2)+0(n) if n>1
By Master Theorem,we shall show that T(n) is (n lg n), where lg n stands for log, n. ro
large enough inputs, mergesort, with its 0(n lg n) running time, outperforms insertion sort, whose
running time is (n), in the worst case

You might also like