C++基礎(chǔ)入門(mén)之模板堆排序(下):堆排序——簡(jiǎn)單的翻譯

上一篇C++基礎(chǔ)入門(mén)之模板堆排序(上):模板上的list的創(chuàng)造與操作

其實(shí)接下來(lái)就很簡(jiǎn)單了淮逻,只要把大眾版的堆排序翻譯過(guò)來(lái)就行了

三. 生造heapSort

一樣是四大函數(shù) (這個(gè)接口搞得有點(diǎn)亂琼懊,將就看一下

  • swap
  • maxHeapify
  • build
  • sort

swap很簡(jiǎn)單,沒(méi)什么可說(shuō)的爬早,一個(gè)swap相當(dāng)于兩次change_list

template<int a, int b, class _list>
struct swap {
    using temp = typename change_list<a, find_list<b, _list>::result::value, _list>::result;
    using result = typename change_list<b, find_list<a, _list>::result::value, temp>::result;
};

有了swap應(yīng)該可以寫(xiě)冒泡排序了


先把簡(jiǎn)單的放上來(lái)哼丈,build函數(shù),里面生造了一個(gè)loop循環(huán)筛严,從i循環(huán)到0醉旦;
因?yàn)樽兞坎豢勺儯杂涀oop也是要有l(wèi)ist這個(gè)參數(shù)的

template<class _list, int heapSize>
struct build {
    template<int i, class _list>
    struct loop {
        using result = typename loop<i - 1, typename maxHeapify<_list, i, heapSize>::result>::result;
    };
    template<class _list>
    struct loop<0, _list> {
        using result = typename maxHeapify<_list, 0, heapSize>::result;
    };
    using result = typename loop<(heapSize / 2), _list>::result;
};

sort也一樣簡(jiǎn)單桨啃,沒(méi)什么好說(shuō)的……留下temp的中間過(guò)程會(huì)比較容易讀懂车胡。

template<class _list, int heapSize>
struct sort {
    using temp = typename build<_list, heapSize>::result;
    template<int i, class _list>
    struct loop {
        using temp = typename swap<0, i, _list>::result;
        using temp2 = typename maxHeapify<temp, 0, i>::result;
        using result = typename loop<i - 1, temp2>::result;
    };
    template<class _list>
    struct loop<1, _list> {
        using temp = typename swap<0, 1, _list>::result;
        using result = typename maxHeapify<temp, 0, 1>::result;
    };
    using result = typename loop<(heapSize - 1), _list>::result;
};

最復(fù)雜的是這個(gè)東西, 對(duì)比一下大眾版的堆排序

void maxHeapify(T arr[], int index, int heapSize) {
  int maxIndex = index;
  int left = 2 * index + 1;
  int right = left + 1;
  if (left < heapSize && arr[index] < arr[left]) {
    maxIndex = left;
  }
  if (right < heapSize && arr[maxIndex] < arr[right]) {
    maxIndex = right;
  }
  if (maxIndex != index) {
    swap(arr[maxIndex], arr[index]);
    maxHeapify(arr, maxIndex, heapSize);
  }
    return;
}

其中這個(gè)
if (left < heapSize && arr[index] < arr[left]) {
····maxIndex = left;
}
if (right < heapSize && arr[maxIndex] < arr[right]) {
····maxIndex = right;
}
我發(fā)現(xiàn)如果去做個(gè)通常版本的if_then_else像這樣不一定能用照瘾,訪(fǎng)問(wèn)仍有會(huì)過(guò)界的匈棘,所以只好把結(jié)果都丟到元函數(shù)類(lèi)中的元函數(shù)類(lèi)了

template<bool Cond, class Then, class Else>
struct If{using result = Then;};
template<bool Cond, class Then, class Else>
struct If<false>{using result = Else;};

以上并不那么好用,所以還是根據(jù)實(shí)際情況生造了一個(gè)并且把if判斷做成嵌套
if (left < heapSize) {
····if(arr[index] < arr[left])
········maxIndex = left;
····else
········maxIndex = index;
}
else
····maxIndex = index;

(現(xiàn)在我已經(jīng)不想看它了……好復(fù)雜

template<class _list, int index, int heapSize>
struct maxHeapify {
    static const int left = 2 * index + 1;
    static const int right = left + 1;
    template<bool cond, int index, int num1>
    struct if_temp {
        static const int max = index;
    };
    template<int index, int num1>
    struct if_temp<true, index, num1> {
        template<bool cond, int index, int num1>
        struct if_temp2 {
            static const int max = index;
        };
        template<int index, int num1>
        struct if_temp2<true, index, num1> {
            static const int max = num1;
        };
        static const int max = if_temp2 < ((find_list<index, _list>::result::value) < (find_list<num1, _list>::result::value)), index, num1>::max;
    };
    static const int temp_max = if_temp<(left < heapSize), index, left>::max;
    static const int maxIndex = if_temp<(right < heapSize), temp_max, right>::max;

    template<bool cond1, int maxIndex, int index, int heapSize>
    struct if_temp3 {};
    template<int maxIndex, int index, int heapSize>
    struct if_temp3<false, maxIndex, index, heapSize> {
        using result = _list;
    };
    template<int maxIndex, int index, int heapSize>
    struct if_temp3<true, maxIndex, index, heapSize> {
        using temp_list = typename swap<maxIndex, index, _list>::result;
        using result = typename maxHeapify<temp_list, maxIndex, heapSize>::result;
    };
    using result = typename if_temp3<(maxIndex != index), maxIndex, index, heapSize>::result;
};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末析命,一起剝皮案震驚了整個(gè)濱河市主卫,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌鹃愤,老刑警劉巖簇搅,帶你破解...
    沈念sama閱讀 222,104評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異软吐,居然都是意外死亡馍资,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)鸟蟹,“玉大人乌妙,你說(shuō)我怎么就攤上這事〗ㄔ浚” “怎么了藤韵?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,697評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)熊经。 經(jīng)常有香客問(wèn)我泽艘,道長(zhǎng),這世上最難降的妖魔是什么镐依? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,836評(píng)論 1 298
  • 正文 為了忘掉前任匹涮,我火速辦了婚禮,結(jié)果婚禮上槐壳,老公的妹妹穿的比我還像新娘然低。我一直安慰自己,他們只是感情好务唐,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,851評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布雳攘。 她就那樣靜靜地躺著,像睡著了一般枫笛。 火紅的嫁衣襯著肌膚如雪吨灭。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,441評(píng)論 1 310
  • 那天刑巧,我揣著相機(jī)與錄音喧兄,去河邊找鬼。 笑死啊楚,一個(gè)胖子當(dāng)著我的面吹牛吠冤,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播特幔,決...
    沈念sama閱讀 40,992評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼闸昨!你這毒婦竟也來(lái)了蚯斯?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,899評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤饵较,失蹤者是張志新(化名)和其女友劉穎拍嵌,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體循诉,經(jīng)...
    沈念sama閱讀 46,457評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡横辆,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,529評(píng)論 3 341
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了茄猫。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片狈蚤。...
    茶點(diǎn)故事閱讀 40,664評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡困肩,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出脆侮,到底是詐尸還是另有隱情锌畸,我是刑警寧澤,帶...
    沈念sama閱讀 36,346評(píng)論 5 350
  • 正文 年R本政府宣布靖避,位于F島的核電站潭枣,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏幻捏。R本人自食惡果不足惜盆犁,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,025評(píng)論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望篡九。 院中可真熱鬧谐岁,春花似錦、人聲如沸瓮下。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,511評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)讽坏。三九已至锭魔,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間路呜,已是汗流浹背迷捧。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,611評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留胀葱,地道東北人漠秋。 一個(gè)月前我還...
    沈念sama閱讀 49,081評(píng)論 3 377
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像抵屿,于是被迫代替她去往敵國(guó)和親庆锦。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,675評(píng)論 2 359