You are on page 1of 25

Merge Sort

16-10-2012

Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat

Collator
Merging the cards Input:
Two separate decks of sorted card

Output:
Single sorted deck of cards

16-10-2012

Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat

Started with, Merging of two ordered lists in a single sorted list List 1: 11 23 42 List 2: 9 25 Repeat the following steps: Select the smallest key from List1 and List2 Place it in a new list List: 9 11 23 25 42

Concept: Based on divide-and-conquer approach Derived from real life ways of solving problems
16-10-2012 Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat 3

The Divide-and-Conquer Paradigm:


Divide: Divide the problem into a number of sub-problems.

divide the n-element sequence(problem) into two sequences (sub-problems) of n/2 elements each. Conquer:
Combine:

If the sub-problem sizes are small enough, solve them directly. Otherwise solve them recursively.
Combine the solutions to the sub-problems into the solution for the original problem.

Merge Sort : divide-and-conquer approach Insertion Sort : Incremental approach


16-10-2012 Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat 4

Suppose there are some people called Mr. MergeSort. They are identical. They dont know how to do sorting.
But each of them has a secretary called Mr. Merge, who can merge 2 sorted sequences into one sorted sequence.

16-10-2012

Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat

16-10-2012

Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat

16-10-2012

Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat

The procedure MERGE-SORT(A, p, r) sorts the elements in the subarray A[ pr].


The divide step simply computes an index q that partitions

A[ pr] into two sub-arrays: A[ pq], containing n/2 elements, and A[ q + 1r], containing n/2 elements.

16-10-2012

Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat

The MERGE-SORT(A, p, r) procedure sorts the elements A[p....r]: x : Floor The least integer greater than x x : Ceiling The greatest integer less than x
MERGE-SORT (A, p, r) 1 if p < r 2 Then q (p+r)/2 3 MERGE-SORT(A, p, q) 4 MERGE-SORT(A, q+1, r) 5 MERGE (A, p, q, r) If array index consider from 0 then consider Ceiling of (p+r)/2 If array index consider from 1 then consider floor of (p+r)/2

16-10-2012

Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat

Algorithm MERGE (A, p, q, r): i is pointing to left partition 1. let i = p and j = q+1 and k = p j is pointing to right partition 2. while (i <= q) and (j <= r) do if A[i] <= A[j] then B[k] = A[i] Comparing elements of left and i = i + 1, k = k + 1 right partition else B[k] = A[j] j = j + 1, k = k + 1 3. While(i<=q) Copy remaining elements of left do B[k] = A[i] partition k = k + 1 , i=i+1 4. While(j<=r) Copy remaining elements of right do B[k] = A[j] partition k = k + 1 , j=j+1 5. for i = p to k do A[i] = B[i] Copy sorted elements again into original array 16-10-2012 Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat 6. return

10

DIVIDE

10
0

9
1

8
2

7
3

6
4

5
5

p=0, r=5, q=2

P=0, q=2, r=5

p=3 (q+1), r=5,

10
0

9
1

8
2 3

6
4

5
5 p=5 (q+1), r=5

p=0, r=2, q=1

p=0, q+1=2, r=2

p=3, q=4

10

8
2

7
3 p=3, q=3

6
4

5
5

0 1 p=0, r=1, q=0

p=0, r=1, q+1=1

p=4 (q+1), r=4

10
16-10-2012

7
Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat

6
11

Sorted

A:
merge
Sorted Sorted

L:
16-10-2012

R:
Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat 12

p=0, r=1, q=0

p=0, r=1, q+1=1

A:

10

3 9
k=0

10 15 28

10 14
i=0, j=1, k=0

L: 10
i=0
16-10-2012

R: 9
j=0
Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat 13

A:

L:

10

R: 5

16-10-2012

Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat

14

A:

3 5
k=0

15 28

10 14

L:

8
i=0

10

R: 5
j=0

16-10-2012

Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat

15

A:

3 5

6
k=1

15 28

10 14

L:

10

R: 5

6
j=1

i=0
16-10-2012

Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat

16

A:

3 5

7
k=2

28

10 14

L:

10

R: 5

7
j=2

i=0
16-10-2012

Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat

17

A:

3 5

8
k=3

10 14

L:

8
i=0

10

R: 5

7
j=3

16-10-2012

Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat

18

A:

3 5

9
k=4

10 14

L:

9
i=1

10

R: 5

7
j=3

16-10-2012

Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat

19

A:

3 5

10
k=5

L:

10
i=2

R: 5

7
j=3

16-10-2012

Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat

20

Assumption: N is a power of two For N = 1 time is constant (denoted by 1)


Otherwise:

time to mergesort N elements = time to mergesort N/2 elements + time to merge two arrays each N/2 el.

16-10-2012

Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat

21

Merge-Sort Analysis
Time, divide
n
n

n/2

n/2

2 n/2 = n
log n levels

n/4

n/4

n/4

n/4

4 n/4 = n

n1=n
Total time for divide: n log n

16-10-2012

Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat

22

Analysis of MERGE Procedure


Algorithm MERGE (A, p, q, r):

1. let i = p and j = q+1 and k = p 2. while (i <= q) and (j <= r)


do if A[i] <= A[j] then B[k] = A[i] i = i + 1, k = k + 1 else B[k] = A[j] j = j + 1, k = k + 1

Line 1 take constant time


In line 2 loop iterates

maximum n

times
In line 3 - 4 takes less than n times In line 5 loop iterates

3. While(i<=q)
do B[k] = A[i] k = k + 1 , i=i+1

maximum n

times
Conclusion: The MERGE procedure

4. While(j<=r)
do B[k] = A[j] k = k + 1 , j=j+1

runs in 1 + n + n = n time.

5. for i = p to k
do A[i] = B[i]

6. return
16-10-2012 Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat 23

Total time for merge sort Time for divide + time for merge n log n + n

16-10-2012

Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat

24

16-10-2012

Reema Patel, Data Structure, B.Tech-II - 2012, NIT-Surat

25

You might also like