Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 65

DATA STRUCTURES AND ALGORITHMS

MODULE 5

MERGE SORT

MERGESORT
Process
1. Divide phase:

The list of N elements is

first divided into two sublists of N/2 elements

further divided into sublists containing N/4 elements

and so on until each sublist contains only one element.


2. Conquer phase

Each half is sorted independently


The 2 sorted halves are merged to a sorted sequence

DIVIDE PHASE
N elements

N/2 elements

N/4 elements

N/4 elements

1 element 1 element 1 element

N/2 elements

N/4 elements

N/4 elements

1 element 1 element 1 element

CONQUER PHASE

Now merge adjacent sublist of one element taking two sublists at a time.

Repeat this process until there is only one array remaining of size N
1 element1 element1 element
N/4 elements

N/4 elements

1 element1 element1 element


N/4 elements

N/2 elements

N/4 elements

N/2 elements
N elements

Eg: DIVIDE

12 7 18 22 5 16 4 10

Eg: DIVIDE
12

12

18

18

22

22

16

16

10

10

Eg: DIVIDE
12

12

12

18

18

22

22

18

22

16

16

16

10

10

10

Eg: DIVIDE
12

12

12

12

18

18

22

22

18

18

22

22

16

16

10

10

16

16

10

10

Eg: CONQUER

12

18

22

16

10

Eg: CONQUER

12

12

18

22

18 22

16

16

10

10

Eg: CONQUER

12

12

12

22

18

18 22

18

22

16

16

10

10

10

16

Eg: CONQUER

12

12

12

22

18

18 22

18

22

16

16

7 10 12 16 18 22

10

10

10

16

HOW IS IT DONE ??

INPUT ARRAY

11
LEFT=0

15

6
RIGHT=8

FIND MID POSITION

11

0
LEFT=0

15

MID = (LEFT + RIGHT ) /2


= (0 +8 ) /2
=4

6
8
RIGHT=8

FIND MID POSITION

11

LEFT=0

15

MID=4

MID = (LEFT + RIGHT ) /2


= (0 +8 ) /2
=4

6
RIGHT=8
8

SPLIT THE ARRAY INTO TWO

11

15

8
RIGHT=8

LEFT=0

11

LEFT=0

MID=4

15
5
MID+1=5

8
RIGHT=8

SORT INDEPENDENTLY

11

4
MID=4

LEFT=0

1
5
MID+1=5

15

8
RIGHT=8

MERGE

11

4
MID=4

LEFT=0

15

MID+1=5

RIGHT=8

11

15

0
LEFT=0

8
RIGHT=8

MID=4

MID+1=5

Algorithm Mergesort(left,right)

1. if (left<right)
1. mid = (left+right)/2
2.

mergesort(left,mid)

3.

mergesort (mid+1,right)

4.

arraymerge (left,mid,right)

2. endif

// more than 1 element


// Divide
// Conquer

HOW TO MERGE 2 SORTED ARRAYS?

3
LEFT

MID1+1
LEFT

Temporary Array

11
MID

15
RIGHT

Process of Merging 2 Sorted Arrays

3
LEFT

MID1+1
LEFT

Temporary Array

11
MID

15

RIGHT

Process of Merging 2 Sorted Arrays

3
LEFT

MID1+1
LEFT

1
Temporary Array

11
MID

15

RIGHT

Process of Merging 2 Sorted Arrays

3
LEFT

MID1+1
LEFT

1
Temporary Array

11
MID

15

RIGHT

Process of Merging 2 Sorted Arrays

3
LEFT

MID1+1

LEFT

Temporary Array

11
MID

15

RIGHT

Process of Merging 2 Sorted Arrays

3
LEFT

MID1+1

LEFT

Temporary Array

11

MID

15

RIGHT

Process of Merging 2 Sorted Arrays

3
LEFT

MID1+1

LEFT

Temporary Array

11

MID

15

RIGHT

Process of Merging 2 Sorted Arrays

3
LEFT

MID1+1

LEFT

Temporary Array

11

MID

15

RIGHT

Process of Merging 2 Sorted Arrays

3
LEFT

MID1+1

LEFT

Temporary Array

11

MID

15

RIGHT

Process of Merging 2 Sorted Arrays

3
LEFT

MID1+1

LEFT

Temporary Array

11

MID

15

RIGHT

Process of Merging 2 Sorted Arrays

3
LEFT

MID1+1

LEFT

Temporary Array

11

MID

15

RIGHT

Process of Merging 2 Sorted Arrays

3
LEFT

MID1+1

LEFT

Temporary Array

11

MID

15

RIGHT

Process of Merging 2 Sorted Arrays

3
LEFT

MID1+1

11

MID

15

RIGHT

LEFT

Process of Merging 2 Sorted Arrays

3
LEFT

MID1+1

11
MID

15

RIGHT

LEFT

Temporary Array

Process of Merging 2 Sorted Arrays

3
LEFT

MID1+1

11
MID

15

RIGHT

LEFT

Temporary Array

11

Process of Merging 2 Sorted Arrays

3
LEFT

MID1+1

11
MID

15

RIGHT

LEFT

Temporary Array

11

15

Process of Merging 2 Sorted Arrays

3
LEFT

MID1+1

11
MID

15

RIGHT

LEFT

Temporary Array

11

15

Copy from temporary Array to original Array

11

15

Temporary Array

MID +1

1
LEFT

LEFT

RIGHT

MID

6
MID

7
MID + 1

11

15
RIGHT

Algorithm: Merge(left,mid,right)

4.

1. set i=left, j=mid+1, k=left


2. while (i<=mid and j<=right)
1. if (array[i]<array[j])
1. temp[k]=array[i]
2. i++
2. else
1. temp[k]=array[j]
2. j++
3. endif
4. k++
3. endwhile

5.
6.

7.
8.
9.

while(i<=mid)
1. temp[k]=array[i]
2. i++
3. k++
endwhile
while(j<=right)
1. temp[k]=array2[j]
2. j++
3. k++
endwhile

i=left
while(i<=right)
1. array[i]=temp[i]
2. i++
10. endwhile

Why set i=left,j=mid+1, k=left

MS(0,5)
Left=0,Right=5
Mid=2

12 7 18 22 5 16
0

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

12 7 18 22 5 16
0

MS(3,5)
Left=3,Right=5
Mid=4

Merge(0,2,5)
Left=0, Mid=2,
Right=5

12 7 18 22 5 16

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

MS(3,5)
Left=3,Right=5
Mid=4

Merge(0,1,2)
Left=0, Mid=1
, Right=2

MS(0,1)
Left=0,Right=1
Mid=0
MS(2,2)
Left=2,Right=2
return

Merge(0,2,5)
Left=0, Mid=2,
Right=5

12 7 18 22 5 16

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

MS(3,5)
Left=3,Right=5
Mid=4

Merge(0,1,2)
Left=0, Mid=1
, Right=2

MS(0,1)
Left=0,Right=1
Mid=0
MS(2,2)
Left=2,Right=2
return

MS(0,0)
Left=0,Right=0
return

Merge(0,0,1)
Left=0, Mid=0
, Right=1
MS(1,1)
Left=1,Right=1
return

Merge(0,2,5)
Left=0, Mid=2,
Right=5

12 7 18 22 5 16

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

MS(3,5)
Left=3,Right=5
Mid=4

Merge(0,1,2)
Left=0, Mid=1
, Right=2

MS(0,1)
Left=0,Right=1
Mid=0
MS(2,2)
Left=2,Right=2
return

MS(0,0)
Left=0,Right=0
return

12
0

Merge(0,0,1)
Left=0, Mid=0
, Right=1
MS(1,1)
Left=1,Right=1
return

Merge(0,2,5)
Left=0, Mid=2,
Right=5

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

Merge(0,1,2)
Left=0, Mid=1
, Right=2
MS(2,2)
Left=2,Right=2
return

12
0

18

22

16

12

18

22

16

MS(3,5)
Left=3,Right=5
Mid=4

MS(0,1)
Left=0,Right=1
Mid=0

MS(0,0)
Left=0,Right=0
return

12

Merge(0,0,1)
Left=0, Mid=0
, Right=1
MS(1,1)
Left=1,Right=1
return

Merge(0,2,5)
Left=0, Mid=2,
Right=5

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

Merge(0,1,2)
Left=0, Mid=1
, Right=2
MS(2,2)
Left=2,Right=2
return

12
0

18

22

16

12

18

22

16

MS(3,5)
Left=3,Right=5
Mid=4

MS(0,1)
Left=0,Right=1
Mid=0

MS(0,0)
Left=0,Right=0
return

12

Merge(0,0,1)
Left=0, Mid=0
, Right=1
MS(1,1)
Left=1,Right=1
return

12

Merge(0,2,5)
Left=0, Mid=2,
Right=5

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

12

18

22

16

12

18

22

16

MS(3,5)
Left=3,Right=5
Mid=4

Merge(0,1,2)
Left=0, Mid=1
, Right=2

MS(0,1)
Left=0,Right=1
Mid=0

12

MS(2,2)
Left=2,Right=2
return

Merge(0,2,5)
Left=0, Mid=2,
Right=5

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

12

18

22

16

12

18

22

16

MS(3,5)
Left=3,Right=5
Mid=4

Merge(0,1,2)
Left=0, Mid=1
, Right=2

MS(0,1)
Left=0,Right=1
Mid=0

12

MS(2,2)
Left=2,Right=2
return

18
2

Merge(0,2,5)
Left=0, Mid=2,
Right=5

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

12

18

22

16

12

18

22

16

MS(3,5)
Left=3,Right=5
Mid=4

Merge(0,1,2)
Left=0, Mid=1
, Right=2

MS(0,1)
Left=0,Right=1
Mid=0

12

MS(2,2)
Left=2,Right=2
return

18
2

12

18

Merge(0,2,5)
Left=0, Mid=2,
Right=5

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

12

18

12

18

22

16

12

18

22

16

MS(3,5)
Left=3,Right=5
Mid=4

Merge(0,2,5)
Left=0, Mid=2,
Right=5

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

12

18

12

18

22

16

12

18

22

16

Merge(0,2,5)
Left=0, Mid=2,
Right=5

MS(3,5)
Left=3,Right=5
Mid=4

MS(3,4)
Left=3,Right=4
Mid=3

Merge(3,4,5)
Left=0, Mid=3
, Right=7
MS(5,5)
Left=5,Right=5
return

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

12

18

12

18

22

16

12

18

22

16

Merge(0,2,5)
Left=0, Mid=2,
Right=5

MS(3,5)
Left=3,Right=5
Mid=4

MS(3,4)
Left=3,Right=4
Mid=3

Merge(3,4,5)
Left=0, Mid=3
, Right=7
MS(5,5)
Left=5,Right=5
return

MS(3,3)
Left=3,Right=3
return

Merge(3,3,4)
Left=3, Mid=3
, Right=4
MS(4,4)
Left=4,Right=4
return

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

12

18

12

18

22

16

12

18

22

16

Merge(0,2,5)
Left=0, Mid=2,
Right=5

MS(3,5)
Left=3,Right=5
Mid=4

MS(3,4)
Left=3,Right=4
Mid=3

Merge(3,4,5)
Left=0, Mid=3
, Right=7
MS(5,5)
Left=5,Right=5
return

MS(3,3)
Left=3,Right=3
return

22
3

Merge(3,3,4)
Left=3, Mid=3
, Right=4
MS(4,4)
Left=4,Right=4
return

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

12

18

12

18

22

16

12

18

22

16

Merge(0,2,5)
Left=0, Mid=2,
Right=5

MS(3,5)
Left=3,Right=5
Mid=4

MS(3,4)
Left=3,Right=4
Mid=3

Merge(3,4,5)
Left=0, Mid=3
, Right=7
MS(5,5)
Left=5,Right=5
return

MS(3,3)
Left=3,Right=3
return

22
3

Merge(3,3,4)
Left=3, Mid=3
, Right=4
MS(4,4)
Left=4,Right=4
return

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

12

18

12

18

22

16

12

18

22

16

Merge(0,2,5)
Left=0, Mid=2,
Right=5

MS(3,5)
Left=3,Right=5
Mid=4

MS(3,4)
Left=3,Right=4
Mid=3

Merge(3,4,5)
Left=0, Mid=3
, Right=7
MS(5,5)
Left=5,Right=5
return

MS(3,3)
Left=3,Right=3
return

22
3

Merge(3,3,4)
Left=3, Mid=3
, Right=4
MS(4,4)
Left=4,Right=4
return

22

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

12

18

12

18

22

16

12

18

22

16

Merge(0,2,5)
Left=0, Mid=2,
Right=5

MS(3,5)
Left=3,Right=5
Mid=4

MS(3,4)
Left=3,Right=4
Mid=3

22

Merge(3,4,5)
Left=0, Mid=3
, Right=7
MS(5,5)
Left=5,Right=5
return

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

12

18

12

18

22

16

12

18

22

16

Merge(0,2,5)
Left=0, Mid=2,
Right=5

MS(3,5)
Left=3,Right=5
Mid=4

MS(3,4)
Left=3,Right=4
Mid=3

22

Merge(3,4,5)
Left=0, Mid=3
, Right=7
MS(5,5)
Left=5,Right=5
return

16
5

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

12

18

12

18

22

16

12

18

16

22

Merge(0,2,5)
Left=0, Mid=2,
Right=5

MS(3,5)
Left=3,Right=5
Mid=4

MS(3,4)
Left=3,Right=4
Mid=3

22

16 22

Merge(3,4,5)
Left=0, Mid=3
, Right=7
MS(5,5)
Left=5,Right=5
return

16
5

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

12

18

12

18

22

16

12

18

16

22

16 22
4

MS(3,5)
Left=3,Right=5
Mid=4

Merge(0,2,5)
Left=0, Mid=2,
Right=5

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

12

18

12

18

22

16

12

18

16

22

16 22
4

Merge(0,2,5)
Left=0, Mid=2,
Right=5

MS(3,5)
Left=3,Right=5
Mid=4

12

18

16

22

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

12

18

22

16

12

18

16

22

Merge(0,2,5)
Left=0, Mid=2,
Right=5

MS(3,5)
Left=3,Right=5
Mid=4

12

18

16

22

MS(0,5)
Left=0,Right=5
Mid=2

MS(0,2)
Left=0,Right=2
Mid=1

12

18

22

16

12

16

18

22

Merge(0,1,2)
Left=0, Mid=1
, Right=2

MS(0,1)
Left=0,Right=1
Mid=0

MS(3,4)
Left=3,Right=4
Mid=3

MS(2,2)
Left=2,Right=2
return

MS(0,0)
Left=0,Right=0
return

Merge(0,2,5)
Left=0, Mid=2,
Right=5

MS(3,5)
Left=3,Right=5
Mid=4

Merge(0,1,1)
Left=0, Mid=0
, Right=1
MS(1,1)
Left=1,Right=1
return

Merge(3,4,5)
Left=0, Mid=3
, Right=7
MS(5,5)
Left=5,Right=5
return

MS(3,3)
Left=3,Right=3
return

Merge(3,3,4)
Left=3, Mid=3
, Right=4
MS(4,4)
Left=4,Right=4
return

SORT 9, 8, 7, 6, 5, 4, 3, 2, 1

Sort

Average

Worst

Comparison

Extra
Memory

bubble

O (N2)

O (N2)

poor

No

selection

O (N2)

O (N2)

fair

No

insertion

O (N2)

O (N2)

good

No

quicksort

O (N log N) O (N2)

good

No

mergesort O (N log N) O (N log N) fair

Yes

You might also like