Merge Sort
Merge Sort
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
Initial sequence
Figure 5.1
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
1<4 so 4--25|-2
9=2
Here p=1
r=2
1<2 then
n, =2-1
n, =1
100 ALGORITHMS DESIGN AND ANALYSIS
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
Here p=3
r=4
3<4 then
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
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
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
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
5 10 15 20 25 30 35 40
5 10 15 20 25 30 35 40