算法导论之Divide and conquer分治思想中的merge算法实现

Rewrite the MERGE procedure so that it does not use sentinels, instead stopping once either array L or RR has had all its elements copied back to A and then copying the remainder of the other array back into A.(分治思想算法里的组合,不用最大定位值为无限大,可以采取的代码写法)

n1 = q - p + 1
n2 = r - q
create arrays L[1..n1] and R[1..n2]
for i = 1 to n1
  L[i] = A[p + i - 1]
for j = 1 to n2
  R[j] = A[q + j]
i = 1
j = 1
for k = p to r
  if i > n1
      A[k] = R[j]
      j = j + 1
  else if j > n2
      A[k] = L[i]
      i = i + 1
  else if L[i] ≤ R[j]
      A[k] = L[i]
      i = i + 1
  else
      A[k] = R[j]
      j = j + 1

添加链接描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值