您的位置:首页 > 编程语言 > Go语言

Introduction to Algorithm - Summary of Chapter 2(2) - Merge Sort

2015-11-12 17:29 621 查看
merge sort :Merge the two sorted subsequences to produce the sorted sequence recursively to the original problem.

Merge (A, p, q, r)
n1 = q-p+1
n2 = r-q
let L[1..n1+1] and R[1..n2+1] be new array
for i=1 to n1
L[i] = A[p+i-1]
for i=1 to n2
R[i] = A[q+i]
L[n1+1] = R[n2+1] = INF
i = 1
j = 1
for k=p to r
if L[i] <= 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] // floor
Merge-Sort (A, p, q)
Merge-Sort (A, q+1, r)
Merge (A, p, q, r)


The merge sort takes running time of Θ(nlgn), It is stable but not in place

Some of above content refere to “Introduction to Algorithm”.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息