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

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

THe show of Merge execution

continue

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”.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值