[旧文整理]排序算法之归并排序(Merge Sort)

FunctionGHW posted @ 2013年9月23日 20:43 in 算法学习 with tags c merge sort in-place , 3930 阅读

归并排序(Merge Sort)是分治法的一个典型应用,相对于冒泡排序、插入排序和选择排序,其性能上有所提升,同时也是基于比较的排序算法中最好的算法之一。


  1. (Divide):将待排序序列(原问题)分成两个规模大致相等的子序列(子问题)
  2. (Conquer):递归地排序两个子序列,当子序列规模足够小的时候直接完成排序
  3. (Combine):合并两个已序的子序列得到原序列的排序结果




void merge_sort(int arr[], size_t size)
    if (size < 2)
    size_t mid = size >> 1;   //划分点

    merge_sort(arr, mid);           //递归地排序左子序列
    merge_sort(arr + mid, size - mid);//递归地排序右子序列

    merge(arr, size, mid);          //合并两个已序的子序列



void merge(int arr[], size_t size, size_t mid)
    size_t lft_s = 0,   //左子序列起始点
           rit_s = mid, //右子序列起始点。左右两个子序列各自是已序的。
           pos = 0,

    //排序后存放结果的 临时数组,别忘了复制回原数组(arr[])!
    int* buf = (int*)malloc(size * sizeof(int));
    while (lft_s < mid && rit_s < size)
        if (arr[lft_s] < arr[rit_s])
            buf[pos++] = arr[lft_s++];
            buf[pos++] = arr[rit_s++];
    while (lft_s < mid)//左子序列有剩余元素
        buf[pos++] = arr[lft_s++];
    while (rit_s < size)//右子序列有剩余元素
        buf[pos++] = arr[rit_s++];
    for (i = 0; i < size; ++i)
        arr[i] = buf[i];

归并排序的递归分解过程就是个"二分"的过程,因此其时间复杂度为\(O(\log n)\),而Merge函数的时间复杂度是\(O(n)\),因此整个算法的复杂度是\(O(n\log n)\),略过证明。最后注意因为Merge里用到了额外的内存空间,因此这里有个\(O(n)\)的空间开销。

原地归并排序(In-place Merge Sort)



void reverse(int arr[], size_t size) {
    int left = 0,
        right = size - 1,
        tmp = 0;
    while(left < right)
        tmp = arr[left];
        arr[left] = arr[right];
        arr[right] = tmp;

//手摇法 通过三次反转操作交换两个子序列的位置,两个子序列内部的排序不变。
void swap_blocks(int arr[], size_t size, size_t lft_size) {
    reverse(arr, lft_size);
    reverse(arr + lft_size, size - lft_size);
    reverse(arr, size);


void in_place_merge(int arr[], size_t size, size_t mid)
    size_t lft_s = 0, rit_s = mid, rmove;

    while (lft_s < rit_s && rit_s < size)
        while (lft_s < rit_s && arr[lft_s] <= arr[rit_s])
        rmove = 0;
        while (rit_s < size && arr[lft_s] > arr[rit_s])
        swap_blocks(arr + lft_s, rit_s - lft_s, rit_s - lft_s - rmove);
        lft_s += rmove;



合并排序 MergeSort, 终于自己做出来了


Liwovosa 说:
2021年4月22日 16:53

Superior post, keep up with this exceptional work. It's nice to know that this topic is being also covered on this web site so cheers for taking the time to discuss this! Thanks again and again! solarmovies

Liwovosa 说:
2021年4月25日 16:36

I have recently started a blog, the info you provide on this site has helped me greatly. Thanks for all of your time & work.

Liwovosa 说:
2021年4月27日 17:45

i was just browsing along and came upon your blog. just wanted to say good blog and this article really helped me. สมัครFIFA55

Liwovosa 说:
2021年4月28日 16:50 This particular is usually apparently essential and moreover outstanding truth along with for sure fair-minded and moreover admittedly useful My business is looking to find in advance designed for this specific useful stuffs…
jackjohnny 说:
2021年5月01日 17:44

I'm glad I found this web site, I couldn't find any knowledge on this matter prior to.Also operate a site and if you are ever interested in doing some visitor writing for me if possible feel free to let me know, im always look for people to check out my web site. disney+ generatordisney+ accounts

jackjohnny 说:
2021年5月06日 17:59

Thank you for taking the time to publish this information very useful! Visto turistico per l'India

jackjohnny 说:
2021年5月11日 19:38

Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you. how to get free bp and uc in pubg

play club casino 说:
2021年5月12日 19:06

Say you got a nice blog post. Much thanks again. Really Great.<a href=""></a>

play club casino 说:
2021年5月12日 19:07

Say you got a nice blog post. Much thanks again. Really Great.[url=][/url]

jackjohnny 说:
2021年5月20日 16:03

I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work. how to earn money 200 per day

online blackjack 说:
2021年6月11日 00:38

I truly appreciate this post. Really looking forward to read more. Fantastic.

jackjohnny 说:
2021年6月11日 04:18

This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here keep up the good work Citronella oil Manufacturers in India

jackjohnny 说:
2021年6月17日 18:41

What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much. coin master hack

jackjohnny 说:
2021年6月18日 00:24

Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post. 먹튀사이트

jackjohnny 说:
2021年6月20日 19:44

thanks for this usefull article, waiting for this article like this again. Free Diamonds Generator

jackjohnny 说:
2021年6月24日 16:09

I was surfing the Internet for information and came across your blog. I am impressed by the information you have on this blog. It shows how well you understand this subject.

jackjohnny 说:
2021年6月26日 22:53

Thank you for the update, very nice site.. <a href="">안전놀이터</a>

spielautomaten-deuts 说:
2021年6月27日 20:19

Major thanks for the blog post. Really thank you! Keep writing.

jackjohnny 说:
2021年6月29日 23:28

thanks for the tips and information..i really appreciate it.. new zealand tourist visa

arkseo 说:
2021年7月05日 03:46

Your work is truly appreciated round the clock and the globe. It is incredibly a comprehensive and helpful blog. how to become a payment service provider

arkseo 说:
2021年7月06日 12:19

Very good points you wrote here..Great stuff...I think you've made some truly interesting points.Keep up the good work. Free Robots.txt Generator Tool

spielautomaten deuts 说:
2021年7月10日 20:07

Great thanks for sharing this blog post. Thanks Again. Keep writing.

jackjohnny 说:
2021年7月13日 18:19

Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include. Brawl Stars Hack 2021

jackjohnny 说:
2021年7月14日 19:59

ikinci el cikma enjektör bosch kullanilmis oto enjektör satin al ikinci el enjektör

spielautomaten-deuts 说:
2021年7月19日 02:10

Very neat blog post. Really thank you! Will read on...

spielautomaten 说:
2021年7月28日 19:27

Major thanks for the blog article. Really looking forward to read more. Really Cool.

casino portal 说:
2021年8月01日 19:27

Major thanks for the article. Thanks Again.

aman 说:
2021年8月18日 21:38

Really great post. I enjoy reading this article. There is such a true article there is more information for us.
<a href="">4anime</a>

aman 说:
2021年8月18日 21:40

Thanks for sharing this lovely post.

Arkseo 说:
2021年8月30日 13:32

I haven’t any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. Good Search

Arkseo 说:
2021年9月04日 14:37

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is extremely helpful for me. merchant services job description

aman 说:
2021年9月28日 19:50

I read that Post and got it fine and informative. <a href="">9ANIME</a>

download instagram r 说:
2021年10月06日 21:20

Thank you for your article post. Really thank you! Cool.

登录 *

loading captcha image...
or Ctrl+Enter