InterviewSolution
Saved Bookmarks
| 1. |
Suppose A, B, C are arrays of integers of sizes m, n, m+n respectively. The numbers in arrays A and B appear in descending order. Give an algorithm to produce a third array C, containing all the data of array A and B in ascending order. |
|
Answer» Assuming that L = 0 and U = m-1,n-1 and (m+n)-1 respectively for A, B, and C 1. ctrA=m-1; ctrB=n-1; ctrC=0; 2. while ctrA>=0 and ctrB>=0 perform steps 3 through 10 3. { If A[ctrA]<=B[ctrB] then 4. { C[ctrC]=A[ctrA] 5. ctrC=ctrC+1 6. ctrA=ctrA-1 } 7. else 8. { C[ctrC]=B[ctrB] 9. ctrC=ctrC+1 10. ctrB=ctrB-1 } } 11. if ctrA<0 then 12. { while ctrB>=0 perform steps 13 through 15 { 13. C[ctrC]=B[ctrB] 14. ctrC=ctrC+1 15. ctrB=ctrB-1 } } 16. if ctrB<0 then 17. { while ctrA>=0 perform steps 18 through 20 18. { C[ctrC]=A[ctrA] 19. ctrC=ctrC+1 20. ctrA=ctrA-1 } } |
|