多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁(yè) > php開(kāi)源 > php教程 > STL練習(xí)2 實(shí)現(xiàn)插入排序,箱子排序和基數(shù)排序

STL練習(xí)2 實(shí)現(xiàn)插入排序,箱子排序和基數(shù)排序

來(lái)源:程序員人生   發(fā)布時(shí)間:2015-05-11 09:02:29 閱讀次數(shù):2740次

使用list實(shí)現(xiàn)了排序的中比較簡(jiǎn)單的插入排序,箱子排序和基數(shù)排序,其中,箱子排序和基樹(shù)排序只能用于數(shù)的排序,所以限制還是蠻大的,箱子排序在實(shí)際使用中基本上不使用,箱子排序是基數(shù)排序的基礎(chǔ),基數(shù)排序有MSD和LSD,MSD也就是從最高位開(kāi)始向最低位排序,LSD也就是從最低位向最高位排序。

下面附上我的實(shí)現(xiàn)代碼:

//============================================================================ // Name : STLPractice2.cpp // Author : 陳洪波 // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <list> using namespace std; void selecrSort(int test[],int size); void bulletSort(int test[],int size); void radixSort(int test[],int size,int wei); /** * 實(shí)現(xiàn)插入排序,箱子排序和基數(shù)排序 */ int main() { int test[]= {2,1,4,3}; //selecrSort(test,4); // for(int i = 0;i < 4;i++){ // cout << test[i] << " "; // } //bulletSort(test,4); radixSort(test,4,1); for(int m = 0;m < 4;m++){ cout << test[m] << " "; } cout << endl; return 0; } /** * 插入排序 * 假定數(shù)組的第1個(gè)元素是排好序的,則從第2個(gè)元素開(kāi)始 * 與前面的元素進(jìn)行排序,這樣就能夠?qū)崿F(xiàn)排序了 * 例如: 2 1 4 3 * 假定第1個(gè)元素2已排好序了 * 則從第2個(gè)元素1開(kāi)始,向前找,2大于1 * 則將2向后移動(dòng),最后將1插入到2的位置 * 就變成了 1 2 4 3 * 4比2大,則就保持4所在的位置 * 3比4小,則4后移,比2大,則3放在4的位置 * 這樣排序就完成了 * 1 2 3 4 */ void selecrSort(int test[],int size){ if(size == 1) return; int i = 1; for(i = 1;i < size;i++){ if(test[i-1] > test[i]){ int temp = test[i]; int j = i-1; while(j>=0 && test[j] >= temp){ test[j+1] = test[j]; j--; } test[j+1] = temp; } } } //獲得數(shù)組中的最大元素 int Max(int test[],int size){ int i = 0; int max = test[0]; for(i = 1;i < size;i++){ if(test[i] > max) max = test[i]; } return max; } //獲得數(shù)組中的最小元素 int Min(int test[],int size){ int i = 0; int min = test[0]; for(i = 1;i < size;i++){ if(test[i] < min) min = test[i]; } return min; } /** * 箱子排序 * 箱子排序就是相當(dāng)于桶排序,將每個(gè)不1樣大小 * 的數(shù)放入到代表不同數(shù)字的桶中,最后再將桶中的元素順序輸出 */ void bulletSort(int test[],int size){ int max = Max(test,size); int min = Min(test,size); //暫時(shí)使用List list<int> *lists = new list<int>[max-min+1](); int i = 0; for(i = 0;i < size;i++){ lists[test[i]-min].push_back(test[i]); } //輸出 for(i = 0;i < max-min+1;i++){ list<int>::iterator it = lists[i].begin(); cout << *it << " "; } } /** * 基樹(shù)排序(有MSD和LSD) * 現(xiàn)在先實(shí)現(xiàn)最簡(jiǎn)單的基數(shù)排序,就是對(duì)數(shù)字只有從小到大排序,沒(méi)有種別之分 * 基數(shù)排序的思想就是: * 先對(duì)個(gè)位數(shù)字進(jìn)行排序,排序完成以后,統(tǒng)計(jì)成堆 * 接著對(duì)上面排好序的10位數(shù)字進(jìn)行排序,排序完成以后,再進(jìn)行對(duì) * 排好序的百位數(shù)字排序,1次類(lèi)推 */ void radixSort(int test[],int size,int wei){ int i = 0; int m = 0; for(m = 1;m <= wei;m++){ //還是采取list作為桶 list<int> *lists = new list<int>[10]; for(i = 0;i < size;i++){ int temp = test[i]; int loc = 1; int tt = 1; for(tt = 1;tt < m;tt++){ loc *= 10; } lists[(temp/loc%10)].push_back(test[i]); } int j = 0; for(i = 0;i < 10;i++){ if(lists[i].size() != 0){ list<int>::iterator it = lists[i].begin(); while(it != lists[i].end()){ test[j] = *it; it++; j++; } } } } } /** * 用于對(duì)a和b交換 */ void swap(int &a,int &b){ int temp = a; a = b; b = temp; }
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 亚洲一区二区三区久久 | 亚洲国产精品久久久久久 | 中国漂亮护士一级毛片 | ww在线观视频免费观看 | 亚洲天堂女人 | 国产女人成人精品视频 | 免费女人18毛片a级毛片视频 | 亚洲aaaa级特黄毛片 | 日韩色小说 | 91精品久久久久久久久久小网站 | 又污又黄又无遮挡网站 | 日本xxwwwxxxx18| 国产九九热视频 | 日本无玛 | 性欧美日韩 | 牛和人交videos欧美冫3d | 日韩精品片 | 日韩 欧美 自拍 | a免费毛片在线播放 | 天堂69亚洲精品中文字幕 | 日本大胆一区免费视频 | 欧美视频不卡一区二区三区 | 日本一级毛片在线看 | 亚洲精品麻豆 | 国产zzzwww在线观看 | 亚洲一区精品伊人久久伊人 | 视频三区 | 亚洲国产成人精品一区二区三区 | 日本r级在线中文在线播放 日本vs黑人hd | 99亚洲| 日韩三级免费 | 亚洲高清成人 | 看毛片网站 | 成人手机看片 | 羞羞视频免费网站日本 | 亚洲地址一地址二地址三 | 欧美.亚洲.日本一区二区三区 | free 欧美性 free 英国性xxxxhd | 国产一区亚洲一区 | 自拍一区在线 | 亚洲福利在线观看 |