Merge Two Sorted Array :

MERGE SORT () :
A is a sorted array with M elements and B is a sorted array with N elements. C is an empty array with size S , S >= M+N

Step I : [ Initialize Counter ] Set I = J = K = 1
Step II : Repeat Step III to IX While ( I <= M ) and ( J <= N)
Step III : If ( A[I] < B[J]) Then 
Step IV : [ Assign elements of array A to C ]  Set C[K] = A[I]
Step V : Set I = I +1
Step VI :Else
Step VII : [ Assign elements of array B to C ] Set C[K] = B[J] 
Step VIII : Set J =J + 1
 [ End of If ]
Step IX :  Set K = K + 1
[ End of while loop ]
Step X : [ A is Empty ] If (I > M) Then 
Step XI : Repeat Step XII and XIII While ( J <= N )
Step XII :  [ Assign remaining elements of array B to C ] Set C[K] =B[J]
Step XIII : Set J = J + 1 and K = k + 1
                  [ End of while loop ]
         [ End of If ]  
Step XIV : [B is empty ] If ( J > N ) Then 
Step XV : Repeat Step XVI and XVII While ( I <= M )
Step XVI :  [ Assign remaining elements of array A to C ] Set C[K] =A[I]
Step XVII : Set I = I + 1 and K = K + 1
 [End of While loop ]
           [End of If ]
Step XVIII : Exit

Explained Using Figure:
Merge Sort

Leave a comment