计算机算法经典试题参考解答.docVIP

  • 10
  • 0
  • 约4.46千字
  • 约 15页
  • 2023-08-20 发布于浙江
  • 举报
1 给出一个 的算法 解:假设A中元素个数为n,B中元素个数为m. 1. 算法思想:输入两个序列A、B a.分别对A、B进行归并排序 b.同时扫描A、B,设两个指针分别指向A、B的当前扫描元素。初始时i、j分别指向A[0]、B[0] c. 若A[i] B[j]则将A[i]存入C中,i后移 若A[i]B[j] 则将B[j]存入C中,j后移 若A[i]= B[j] 则同时将i、j后移,直到分别出现与其前一个元素值不同的元素为止。 d 重复c直至A、B同时扫描完或某一个扫描完,此时将另一个序列所有剩余元素直接 插入C中即可。 归并排序的时间复杂度为 ,扫描的时间复杂度为 ,总的时间复杂度为 。 2书后习题4.8 a. How many key comparisons would be done in the worst case? b. How many times are elements moved in the worst case? c. What is the asymptotic order of the worst-case running time? d. Can the number of moves be reduced by putting the elements in a linked list instead of an array? Explain. 答案:

文档评论(0)

1亿VIP精品文档

相关文档