Category Archives: Array (1D)

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

Merge two Unsorted Array :

MERGE UNSORT () :
A is an Array with M elements and B is an array with N elements. C is an empty Array with LOC location where LOC >= M+N

Step I     : Repeat For I = 1 to M
Step II   : [ Assign elements of array A to array C ]  Set C[ I ]  = A[ I ]
[ End of For loop ]
Step III  : [ Counter Initialization ] Set K = 1
Step IV  : Repeat For J = M+1 to M+N
Step V   : [ Assign elements of array B to array C ]  Set C[ J ] = B[ K ]
Step VI  : [ Increase counter ] Set K = K + 1
[ End of For loop ]
Step VII : Exit

Delete Item from an Array :

âž¡
ITEM is the element which has to be deleted from location LOC . I is set to LOC from where Item is to be deleted and it iterates to total number of elements i.e. N .

DELETE () :
Here A is a linear array with N elements. LOC is the location and ITEM  is to be deleted.

Step I    : [ Assign the element to be deleted ]  Set ITEM = A[ LOC ]
Step II  : Repeat For I = LOC to N
Step III : Set A[I] = A[I+1]
[ End of For loop ]
Step IV : [ Reset N ]  Set N = N – 1
Step V  : Exit.

Insert Item into Unsorted Array :

âž¡
Here A is an unsorted array stored in memory with N elements.This algo inserts a data element at the loc position in an array.

INSERT_UNSORT() :
A is unsorted array with N elements. LOC is the location where ITEM have to be inserted.

Step I     : [ initialize Counter ] Set I = N
Step II   : Repeat While ( I >= LOC )
Step III  : [ Move elements ] Set A[I + 1] = A[I]
Step IV  : [ Decrease counter ] Set I = I – 1
[ End of while loop ] 
Step V   : [ Insert Element ] Set A[ LOC ] = ITEM
Step VI  : [ Reset N ] Set N = N + 1
Step VII : Exit

Inserting Item into sorted Array :

âž¡
Here A is a sorted array stored in memory. This algo inserts a element a data elements ITEM at ( I + 1)th position . ITEM is compared with each element until it founds its appropriate position.

INSERT_SORT () :
Here A is a sorted linear array ( ascending order ) with N elements. ITEM is to be inserted at appropriate position.

Step I    : [ Initialize counter ]   Set I = N
step II   : Repeat While ( ITEM < A[I] ) and ( I >= 1)
Step III : [ Accessing from last ]   Set A[I+1] = A[I]
Step IV : [ Decreasing Counter ]   Set I = I – 1
[ End of While Loop]
step V   : [Insert Item ]   Set A[I + 1] = ITEM
Step VI : [Reset N]   N = N + 1
Step VII : Exit

Traverse an Array

âž¡
A is the collection of element of same data type.By using this algorithm of traversing we can  print or count all the element of array exactly once.

Traverse () :
This algorithm traverses a linear array  A with lower bound LB and upper bound UB and performs the operation to access each element of the  array.

Step I    :  Repeat For I = LB to UB
Step II   :       Apply Process to A[I]      [ Accessing Element ]
[ End of for loop ]
Step III :  Exit.