演算法學習之排序(6)--合并排序,演算法--

來源:互聯網
上載者:User

演算法學習之排序(6)--合并排序,演算法--
一.演算法描述    歸併排序將一個序列分成兩個長度相等的子序列,為每一個子序列調用歸併排序進行排序,然後再將它們合并成一個序列。合并兩個子序列的過程稱為歸併,歸併排序的已耗用時間並不依賴與輸入數組中元素的組合方式。    歸併排序總是遞迴調用自己對子序列進行排序,遞迴退出的條件為數組中只包含一個元素時,函數不進行其他動作,直接返回。

二.時間複雜度    歸併排序每次都是將序列分成相等的兩個子序列,因此,歸併排序的時間複雜度與輸入數組中的元素是否已經排序無關,在最好情況下,平均情況下和最差情況下的時間複雜度都為O(nlogn)。


三.空間複雜度    歸併排序的歸併過程需要將兩個子序列進行歸併,如果是使用數組之類的結構儲存資料的話,需要將兩個子序列複製到額外的空間中,然後再進行歸併,因此,使用數組作為儲存結構的話,空間複雜度為O(n)。如果是使用的是鏈表作為資料的儲存結構的化,那麼兩個子序列進行歸併的過程只是將鏈表中的元素放到另外一個鏈表中,並不需要複製元素的值,空間複雜度為O(1)。


四.C++實現

#include <iostream>#include "Sort.h"#include "randomarray.h"bool Merge(std::vector<int>::iterator Ite, int LeftIndex, int RightIndex){    if (LeftIndex >= RightIndex)    {        return false;    }    if (LeftIndex == (RightIndex - 1))    {        return true;    }    int Mid = (LeftIndex + RightIndex) / 2;    // copy the array    std::vector<int> LeftArray;    for (int Index = LeftIndex; Index < Mid; Index++)    {        int Tmp = *(Ite + Index);        LeftArray.push_back(*(Ite + Index));    }    std::vector<int> RightArray;    for (int Index = Mid; Index < RightIndex; Index++)    {        int Tmp = *(Ite + Index);        RightArray.push_back(*(Ite + Index));    }    // merge the sub array    for (int Index_left = 0, Index_right = 0, Index = LeftIndex;    Index < RightIndex;    Index++)    {        // Ended merge the left array        if (Index_left >= LeftArray.size())        {            int Tmp = RightArray[Index_right];            *(Ite + Index) = RightArray[Index_right++];            continue;        }        // Ended merge the right array        if (Index_right >= RightArray.size())        {            int Tmp = LeftArray[Index_left];            *(Ite + Index) = LeftArray[Index_left++];            continue;        }        // merge the two array        if (RightArray[Index_right] > LeftArray[Index_left])        {            int Tmp = LeftArray[Index_left];            *(Ite + Index) = LeftArray[Index_left++];        }        else        {            int Tmp = RightArray[Index_right];            *(Ite + Index) = RightArray[Index_right++];        }    }    return true;}bool MergeSort(std::vector<int>::iterator Ite, int LeftIndex, int RightIndex){    // return false if the LeftIndex >= RightIndex    if (LeftIndex >= RightIndex)    {        return false;    }    // return true when there are only one element in the array    if (LeftIndex == (RightIndex - 1))    {        return true;    }    // split the array to two sub array    int Mid = (LeftIndex + RightIndex) / 2;    MergeSort(Ite, LeftIndex, Mid);    MergeSort(Ite, Mid, RightIndex);    // Merge the sub array    return Merge(Ite, LeftIndex, RightIndex);}bool MergeSort(std::vector<int>& Array){    int Size = Array.size();    if (Size == 0)    {        return false;    }    if (Size == 1)    {        return true;    }    return MergeSort(Array.begin(), 0, Size);}




五.參考文獻[1] Shaffer C A. Practical Introduction to Data Structure and Algorithm Analysis (C++ Edition)[J]. 2002.[2] 科曼 and 金貴, 演算法導論: 機械工業出版社, 2006.[3] T. H. Cormen, C. E. Leiserson, R. L. Rivest, and C. Stein, "演算法導論 (影印版)," ed: 北京: 高等教育出版社.





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.