1.

Write an algorithm to merge two arrays X[6], Y[5] stored in descending order. The resultant array should be in ascending order.

Answer»

Assuming that L=0 and U=6-1,5-1 and (6+5)-1 respectively for X, Y, and Z 

1. ctrX=6-1; ctrY=5-1; ctrZ=0; 

2. while ctrX>=0 and ctrY>=0 perform steps 3 through 10 

3. { If X[ctrX]<=Y[ctrY] then 

4. { Z[ctrZ]=X[ctrX] 

5. ctrZ=ctrZ+1 

6. ctrX=ctrX-1 } 

7. else

8. { Z[ctrZ]=Y[ctrY] 

9. ctrZ=ctrZ+1 

10. ctrY=ctrY-1 }

11. if ctrX<0 then 

12. { while ctrY>=0 perform steps 13 through 15

13. Z[ctrZ]=Y[ctrY] 

14. ctrZ=ctrZ+1 

15. ctrY=ctrY-1 

16. if ctrY<0 then 

17. { while ctrX>=0 perform steps 18 through 20

18. { Z[ctrZ]=X[ctrX]

19. ctrZ=ctrZ+1 

20. ctrX=ctrX-1 

}

}



Discussion

No Comment Found

Related InterviewSolutions