Sunday, 18 September 2016

ALGORITHM FOR MERGING OF LINEAR ARRAYS

ALGORITHM FOR MERGING OF LINEAR ARRAYS:
Merging: Merging is the operation of combining the elements of two linear arrays into a single array. The single array created after merging should have enough memory to accommodate all the elements of both the arrays.
Merging is done in sorted and unsorted arrays.

Algorithm for merging two unsorted linear arrays:
           MERGE_UNSORT(A,M,Q,N): Here A is an array with M elements and Q is an array with N elements and R is an array with L elements such that L=M+N.
 1.Repeat for I=1 to M
          R[I]=A[I]
          [End of loop]
2. Set K=1
3. Repeat for J=M+1 to M+N
          Set R[J]=Q[K]
          K=K+1
          [End of loop]
4.Exit.


Algorithm for merging two sorted arrays:
          MERGE_SORTED(A,M,Q,N):  Here A is an array with M elements and Q is an array with N elements and R is an array with L elements such that L=M+N.
1.Set I=J=1
2.Repeat while (I<=M And J<=N)
3. If A[I]<Q[J] then
           R[K]=A[I]
           I=I+1
          J=J+1
4. Else
      R[K]=Q[J]
      J=J+1
5. K=K+1
   [End of If structure ]
   [End of step 2]
6. If   I=M+1  then
       Repeat while J<=N
             R[K]=Q[J]
              K =K+1
             J=J+1
7.If J=N+1 then
          Repeat while I<=P
           R[K]=A[I]
           K=K+1
            I=I+1
            [End of If structure]
8. Exit



No comments:

Post a Comment

POLYMORPHISM IN C++

POLYMORPHISM IN C++ C++ achieves polymorphism through: i) Function overloading ii)Operator overloading By the...