2019-05-20 排序算法

常見排序算法模板實(shí)現(xiàn)


為了使用方便嗜诀,整理了以下幾種排序算法锥咸,沒有過的文字描述以及動畫圖表之類的說明。

#pragma once

#define NULL 0
#define MARK -65535


template<class T>           //聲明
class CDaXiongTemplateSort
{
public:
    CDaXiongTemplateSort();
    ~CDaXiongTemplateSort();
    void SetMember(T*, int );                                                   //初始化數(shù)據(jù)成員
    T*   BubbleSort(bool ascending = true);                                     //冒泡排序 默認(rèn)升序
    T*   SelectionSort(bool ascending = true);                                  //選擇排序
    T*   InsertionSort(bool ascending = true);                                  //插入排序
    T*   ShellSort(bool ascending = true);                                      //希爾排序
    T*   MergeSort(T*, int size, bool ascending = true);                        //歸并排序 start 為起始標(biāo)號,end 為結(jié)束標(biāo)號
    T*   BucketSort(int n);                                                     //桶排序   n表示數(shù)的位數(shù)止潮,只適用于正整數(shù)

protected:
    T *m_num;
    int m_length;
};

template<class T>
CDaXiongTemplateSort<T>::CDaXiongTemplateSort()
{
    this->m_num = nullptr;
}

template<class T>
CDaXiongTemplateSort<T>::~CDaXiongTemplateSort()
{
    if (nullptr != this->m_num)
    {
        delete[]this->m_num;
    }
    this->m_num = nullptr;
}

template<class T>
void CDaXiongTemplateSort<T>::SetMember(T* num, int length)
{
    this->m_num = new T[length];
    this->m_length = length;
    for (int i = 0; i < length; ++i)
    {
        this->m_num[i] = num[i];
    }
}


//冒泡
template<class T>
T* CDaXiongTemplateSort<T>::BubbleSort(bool ascending = true)
{
    
    T tempNum;
    for (int i = 0; i < this->m_length; ++i)
    {
        for (int j = 0; j < this->m_length - 1 - i; ++j)
        {
            if (ascending)
            {
                if (this->m_num[j] > this->m_num[j + 1])
                {
                    tempNum = this->m_num[j];
                    this->m_num[j] = this->m_num[j + 1];
                    this->m_num[j + 1] = tempNum;
                }
                
            }
            else
            {
                if (this->m_num[j] < this->m_num[j + 1])
                {
                    tempNum = this->m_num[j];
                    this->m_num[j] = this->m_num[j + 1];
                    this->m_num[j + 1] = tempNum;
                }
                
            }
            
        }
        
    }
    
    return this->m_num;
}


//選擇
template<class T>
T* CDaXiongTemplateSort<T>::SelectionSort(bool ascending = true)
{
    T tempNum;
    for (int i = 0; i < this->m_length - 1; ++i)
    {
        for (int j = i + 1; j < this->m_length; ++j)
        {
            if (ascending)
            {
                if (this->m_num[i] > this->m_num[j])
                {
                    tempNum = this->m_num[j];
                    this->m_num[j] = this->m_num[i];
                    this->m_num[i] = tempNum;
                }
                
            }
            else
            {
                if (this->m_num[i] < this->m_num[j])
                {
                    tempNum = this->m_num[j];
                    this->m_num[j] = this->m_num[i];
                    this->m_num[i] = tempNum;
                }
                
            }

        }

    }

    return this->m_num;
}

//插入
template<class T>
T* CDaXiongTemplateSort<T>::InsertionSort(bool ascending = true)
{
    T tempNum;
    int mark;
    for (int i = 0; i < this->m_length - 1; ++i)
    {
        tempNum = this->m_num[i + 1];
        for (int j = i + 1; j > 0 ; --j)
        {
            if (ascending)
            {
                if (tempNum < this->m_num[j - 1])
                {
                    this->m_num[j] = this->m_num[j - 1];
                    this->m_num[j - 1] = tempNum;
                }
                
            }
            else
            {
                if (tempNum > this->m_num[j - 1])
                {
                    this->m_num[j] = this->m_num[j - 1];
                    this->m_num[j - 1] = tempNum;
                }
                
            }
            
            
        }
    }
    return this->m_num;
}


//希爾
template<class T>
T* CDaXiongTemplateSort<T>::ShellSort(bool ascending = true)
{
    T tempNum;
    int step = this->m_length / 2;
    while (step > 0)
    {
        for (int i = 0; i < this->m_length - step; ++i)
        {
            tempNum = this->m_num[i + step];
            for (int j = i + step; j > 0 ; j-=step)
            {
                if (ascending)
                {
                    if ((j - step) >= 0 && (tempNum < this->m_num[j - step]))       //此處若用for循環(huán)則要判別 j - srep < 0的情況
                    {
                        this->m_num[j] = this->m_num[j - step];
                        this->m_num[j - step] = tempNum;
                    }
                }
                else
                {
                    if ((j - step) >= 0 && (tempNum > this->m_num[j - step]))       //此處若用for循環(huán)則要判別 j - srep < 0的情況
                    {
                        this->m_num[j] = this->m_num[j - step];
                        this->m_num[j - step] = tempNum;
                    }
                }
                
            }
        }
        step /= 2;
    }
    return this->m_num;
}

//歸并
template<class T>
T* CDaXiongTemplateSort<T>::MergeSort(T* num, int size, bool ascending = true)
{
    
    
    T* lift, *right;
    if (size >> 1)
    {
        lift = new T[size >> 1];
        right = new T[size - (size >> 1)];
        for (int i = 0, j = 0; i < size; ++i)
        {
            if (i < size >> 1)
            {
                lift[i] = num[i];
            }
            else
            {
                right[j++] = num[i];
            }
        }

        MergeSort(lift, size >> 1, ascending);          //排序lift
        MergeSort(right, size - (size >> 1), ascending);    //排序right
        /**************************************************************/

        /**************************************************************/

        int i, j, k;
        for (i = 0, j = 0, k = 0; i < size >> 1 && j < size - (size >> 1);)
        {
            if (ascending)             //升序
            {
                if (lift[i] < right[j])//合并有序
                {
                    num[k++] = lift[i++];
                }
                else
                {
                    num[k++] = right[j++];
                }
            }
            else                        //降序
            {
                if (lift[i] < right[j])//合并有序
                {
                    num[k++] = right[j++];
                }
                else
                {
                    
                    num[k++] = lift[i++];
                }
            }
            
        }
        //剩余元素全部放回去
        if (i >= size >> 1)//說明lift中的內(nèi)容放完了
        {
            while (j < size - (size >> 1))
            {
                num[k++] = right[j++];
            }
        }
        else
        {
            while (i < size >> 1)
            {
                num[k++] = lift[i++];
            }
        }
        delete[] lift;
        delete[] right;
    }

    return num;
}

//桶
template<class T>
T* CDaXiongTemplateSort<T>::BucketSort(int n)
{
    T* bucket[11];                                      //聲明11個桶般哼,第11個桶用來暫存有序數(shù)據(jù)
    int temp;                                           //臨時桶的下標(biāo)
    static int count = 0;
    for (int i = 0;i < 11; ++i)
    {
        bucket[i] = new T[this->m_length];              //定義10 * n 的矩陣用做存儲數(shù)據(jù) n表示需要排序數(shù)組的長度
        for (int j = 0; j < this->m_length; ++j)
        {
            bucket[i][j] = MARK;                        //初始化所有桶元素
        }
    }
    
    for (int i = 0, k = 1; i < n; ++i, k *= 10)//排序的次數(shù) n 最大值的位數(shù)  
    {
        for (int j = 0; j < this->m_length; ++j)//入桶把數(shù)組this->m_num中元素全部倒入桶中
        {
            if (this->m_num[j] < k * 10 && this->m_num[j] >= k)
            {
                temp = this->m_num[j] / k % 10;
                bucket[temp][j] = this->m_num[j];//放入桶中
            }
        }
        //立刻倒回原來的數(shù)組
        for (int m = 0, j = 0; m < 10; ++m) 
        {
            for (int x = 0; x < this->m_length; ++x)//數(shù)據(jù)的個數(shù)
            {
                if (bucket[m][x] != MARK)
                {
                    bucket[10][count++] = bucket[m][x];//倒回桶中
                    bucket[m][x] = MARK;
                }
            }
        }
    }

    for (int i = 0; i < this->m_length; ++i)
    {
        this->m_num[i] = bucket[10][i];
    }

    for (int i = 0; i < 11; ++i)
    {
        delete []bucket[i];
        bucket[i] = NULL;
    }
    return this->m_num;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末吴汪,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子蒸眠,更是在濱河造成了極大的恐慌漾橙,老刑警劉巖,帶你破解...
    沈念sama閱讀 223,126評論 6 520
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件楞卡,死亡現(xiàn)場離奇詭異霜运,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)蒋腮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,421評論 3 400
  • 文/潘曉璐 我一進(jìn)店門淘捡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人池摧,你說我怎么就攤上這事焦除。” “怎么了作彤?”我有些...
    開封第一講書人閱讀 169,941評論 0 366
  • 文/不壞的土叔 我叫張陵踢京,是天一觀的道長。 經(jīng)常有香客問我宦棺,道長瓣距,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,294評論 1 300
  • 正文 為了忘掉前任代咸,我火速辦了婚禮蹈丸,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘呐芥。我一直安慰自己逻杖,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,295評論 6 398
  • 文/花漫 我一把揭開白布思瘟。 她就那樣靜靜地躺著荸百,像睡著了一般。 火紅的嫁衣襯著肌膚如雪滨攻。 梳的紋絲不亂的頭發(fā)上够话,一...
    開封第一講書人閱讀 52,874評論 1 314
  • 那天蓝翰,我揣著相機(jī)與錄音,去河邊找鬼女嘲。 笑死畜份,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的欣尼。 我是一名探鬼主播爆雹,決...
    沈念sama閱讀 41,285評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼愕鼓!你這毒婦竟也來了钙态?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,249評論 0 277
  • 序言:老撾萬榮一對情侶失蹤菇晃,失蹤者是張志新(化名)和其女友劉穎驯绎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體谋旦,經(jīng)...
    沈念sama閱讀 46,760評論 1 321
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,840評論 3 343
  • 正文 我和宋清朗相戀三年屈尼,在試婚紗的時候發(fā)現(xiàn)自己被綠了册着。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,973評論 1 354
  • 序言:一個原本活蹦亂跳的男人離奇死亡脾歧,死狀恐怖甲捏,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情鞭执,我是刑警寧澤司顿,帶...
    沈念sama閱讀 36,631評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站兄纺,受9級特大地震影響大溜,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜估脆,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,315評論 3 336
  • 文/蒙蒙 一钦奋、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧疙赠,春花似錦付材、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,797評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至捍岳,卻和暖如春富寿,著一層夾襖步出監(jiān)牢的瞬間睬隶,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,926評論 1 275
  • 我被黑心中介騙來泰國打工作喘, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留理疙,地道東北人。 一個月前我還...
    沈念sama閱讀 49,431評論 3 379
  • 正文 我出身青樓泞坦,卻偏偏與公主長得像窖贤,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子贰锁,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,982評論 2 361

推薦閱讀更多精彩內(nèi)容

  • 《來客》 郭宏安 譯 小學(xué)教師達(dá)呂望著兩個人朝山上走來赃梧,一個騎馬,一個步行豌熄。學(xué)校建在半山腰上授嘀,他們還沒有爬上門前的...
    黑幫詩人閱讀 989評論 0 2
  • 酸棗樹 原創(chuàng):白秋燕 我的家鄉(xiāng)陜北有一種樹...
    燕子集閱讀 2,231評論 0 2
  • 我與母親之間的鏈接的反思,沒有學(xué)心理學(xué)之前锣险,我一直以為我還算是一個孝順的孩子蹄皱,和父母的關(guān)系很好,通過學(xué)習(xí)之后才知道...
    紫色風(fēng)鈴的簡書閱讀 214評論 0 1