Professional Documents
Culture Documents
Merge Sort: Data Structures and Algorithms
Merge Sort: Data Structures and Algorithms
MODULE 5
MERGE SORT
MERGESORT
Process
1. Divide phase:
DIVIDE PHASE
N elements
N/2 elements
N/4 elements
N/4 elements
N/2 elements
N/4 elements
N/4 elements
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
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
11
0
LEFT=0
15
6
8
RIGHT=8
11
LEFT=0
15
MID=4
6
RIGHT=8
8
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
3
LEFT
MID1+1
LEFT
Temporary Array
11
MID
15
RIGHT
3
LEFT
MID1+1
LEFT
Temporary Array
11
MID
15
RIGHT
3
LEFT
MID1+1
LEFT
1
Temporary Array
11
MID
15
RIGHT
3
LEFT
MID1+1
LEFT
1
Temporary Array
11
MID
15
RIGHT
3
LEFT
MID1+1
LEFT
Temporary Array
11
MID
15
RIGHT
3
LEFT
MID1+1
LEFT
Temporary Array
11
MID
15
RIGHT
3
LEFT
MID1+1
LEFT
Temporary Array
11
MID
15
RIGHT
3
LEFT
MID1+1
LEFT
Temporary Array
11
MID
15
RIGHT
3
LEFT
MID1+1
LEFT
Temporary Array
11
MID
15
RIGHT
3
LEFT
MID1+1
LEFT
Temporary Array
11
MID
15
RIGHT
3
LEFT
MID1+1
LEFT
Temporary Array
11
MID
15
RIGHT
3
LEFT
MID1+1
LEFT
Temporary Array
11
MID
15
RIGHT
3
LEFT
MID1+1
11
MID
15
RIGHT
LEFT
3
LEFT
MID1+1
11
MID
15
RIGHT
LEFT
Temporary Array
3
LEFT
MID1+1
11
MID
15
RIGHT
LEFT
Temporary Array
11
3
LEFT
MID1+1
11
MID
15
RIGHT
LEFT
Temporary Array
11
15
3
LEFT
MID1+1
11
MID
15
RIGHT
LEFT
Temporary Array
11
15
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.
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
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
Yes