排序算法之二分治法

MERGE(A,p,q,r)
n1 = q-p+1
n2 = r-q
let L[1..n_1+1] and R[1..n_2+1] be new arrays
for i = 1 to n_1
  L[i] = A[p+i-1]
for j = 1 to n_2
  R[j] = A[q+j]
L[n_1+1] = \infty
R[n_2+1] = \infty
i = 1
j = 1
for k = p to r
  ifL[i] \leq R[j]
    A[k] = L[i]
    i = i+1
else A[k] = R[j]
    j = j+1

MERGE-SORT(A.p,r)
if p<r
  q = |(p+r)/2|
MERGE-SORT(A,p,q)
MERGE-SORT(A,q+1,r)
MERGE(A,p,q.r)

posted @ 2016-11-20 23:36  且如歌  阅读(141)  评论(0编辑  收藏  举报
TOP