日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

排序相关算法-1

發布時間:2024/4/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 排序相关算法-1 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?因排序代碼過多,同樣分成兩個博文來發表。

  • #include?<iostream>?
  • using?namespace?std;?
  • ?
  • /*?
  • ????希爾排序的分組是gap(增量)的倍數?
  • ????堆排序的分組是?樹(子樹)的分組?
  • */?
  • #define?MAXSIZE?20?
  • #define?MAXD????8?
  • typedef?int?ElemType;?
  • typedef?struct?node?
  • {?
  • ????ElemType?data;?
  • ????struct?node?*next;?
  • }Node;?
  • ?
  • void?CreateList(Node?*&nodes,?int?iarr[],?int?len)????????????//此函數是創建一個單鏈表?
  • {?
  • ????int?loop1?=?0;?
  • ????Node?*temp?=?NULL,?*temp2?=?NULL;?
  • ?
  • ????if(nodes?!=?NULL)?free(nodes);?
  • ????nodes?=?(Node?*)malloc(sizeof(Node));?
  • ????nodes->next?=?NULL;?
  • ????temp2?=?nodes;?
  • ?
  • ????for(loop1?=?0;?loop1?<?len?;?++loop1)?
  • ????{?
  • ????????temp?=?NULL;?
  • ????????temp?=?(Node?*)malloc(sizeof(Node));?
  • ????????temp->data?=?iarr[loop1];?
  • ????????temp->next?=?temp2->next;?
  • ????????temp2->next?=?temp;?
  • ????????temp2?=?temp;?
  • ????}?
  • }?
  • ?
  • void?InsertSort2(Node?*&nodes)???????????????????????//對單鏈表進行排序?
  • {?
  • ????Node?*temp?=?NULL,?*node?=?NULL,?*prenode?=?NULL,?*pre?=?NULL;?
  • ????if(nodes?==?NULL?||?nodes->next?==?NULL?)?
  • ????{?cout<<"wrong?parameter"<<endl;return?;?}?
  • ????else?
  • ????{???pre?=?nodes;?
  • ????????temp?=?nodes->next;?
  • ?????????
  • ????????prenode?=?temp->next;?
  • ????????node?=?temp->next;?
  • ????????temp->next?=?NULL;??????????????????????????//把第三個域的值給置空,???被一個單鏈表分為兩個單鏈表?
  • ?????????
  • ????????while(prenode?!=?NULL)?
  • ????????{??
  • ????????????node?=?prenode;?
  • ????????????pre?=?nodes;?
  • ????????????temp?=?nodes->next;?????
  • ????????????while(temp?!=?NULL?&&?(node->data?>?temp->data))?????????????//判斷的條件的順序不能顛倒,這一點要謹慎?
  • ????????????{????
  • ????????????????pre?=?temp;?????
  • ????????????????temp?=?temp->next;??
  • ????????????}??
  • ????????????prenode?=?prenode->next;?
  • ?????????????
  • ????????????node->next?=?pre->next;?
  • ????????????pre->next?=?node;?????????
  • ????????}?
  • ????}?
  • }?
  • ?
  • void?Disp(Node?*nodes)?
  • {?
  • ????Node?*temp?=?NULL;?
  • ????if(nodes?!=?NULL)?
  • ????{?
  • ????????temp?=?nodes->next;?
  • ????????while(temp?!=?NULL)?
  • ????????{?
  • ????????????cout<<temp->data<<'?';?
  • ????????????temp?=?temp->next;?
  • ????????}?
  • ????}?
  • ????cout<<endl;?
  • }?
  • ?
  • void?Disp(int?iarr[],?int?len)?
  • {?
  • ????int?loop1?=?0;?
  • ????while(loop1?<?len)?
  • ????{?
  • ????????cout<<iarr[loop1]<<'?';?
  • ????????++loop1;?
  • ????}?
  • ????cout<<endl;?
  • }?
  • ?
  • void?Disp(char?iarr[][4],?int?len)?
  • {?
  • ????int?loop1?=?0;?
  • ????while(loop1?<?len)?
  • ????{?
  • ????????cout<<iarr[loop1]<<'?';?
  • ????????++loop1;?
  • ????}?
  • ????cout<<endl;?
  • }?
  • ?
  • ?
  • void?InsertSort(int?iarr[],?int?len)??????//此函數是直接插入排序,從數組中得出一個值,把它按照順序插入到前面的數組中?
  • {?
  • ????int?loop1?=?0,?loop2?=?0,?loop3?=?0,?temp?=?0;?
  • ?
  • ????for(loop1?=?1;?loop1?<?len?;?++loop1)???????//循環從1以后的元素?
  • ????{?
  • ????????temp?=?iarr[loop1];?????????????????????
  • ?
  • ????????for(loop2?=?0;?loop2?<?loop1?&&?temp?>?iarr[loop2];?++loop2);???????//找到要插入的位置?
  • ?
  • ????????loop3?=?loop1;?
  • ????????while(loop3?>=?loop2)????????????????????????//移動元素?
  • ????????{?
  • ????????????iarr[loop3]?=?iarr[loop3?-?1];?
  • ????????????--loop3;?
  • ????????}?
  • ????????iarr[loop2]?=?temp;?
  • ????}?
  • }?
  • ?
  • void?ShellSort(int?iarr[],?int?len)?????????????//此函數又稱為縮小增量,?把記錄按下標的一定在增量gap,其中下標值是gap倍數的元素為一組,對這一組元素進行插入排序?
  • {???????????????????????????????????????????????//每次循環,都把gap縮小到原來的二分之一,?直到其值為0.??????????主要思想是其分組的方式是把?增量的倍數為一組?
  • ????int?loop1?=?0,?loop2?=?0,?temp?=?0,?gap?=?0;?
  • ?????
  • ????gap?=?len?/?2;?
  • ????while(gap?>?0)?
  • ????{?
  • ????????for(loop1?=?gap;?loop1?<?len?;?++loop1)?
  • ????????{?
  • ????????????temp?=?iarr[loop1];?
  • ?
  • ????????????loop2?=?loop1?-?gap;?
  • ????????????while(loop2?>=?0?&&?iarr[loop2]?>?temp)????????????//此循環就相當于一個插入排序?
  • ????????????{?
  • ????????????????iarr[loop2?+?gap]?=?iarr[loop2];?
  • ????????????????loop2?=?loop2?-?gap;?
  • ????????????}?
  • ?????????????
  • ????????????iarr[loop2?+?gap]?=?temp;?
  • ??????????
  • ????????}?
  • ????????gap?=?gap?/?2;?
  • ????}?
  • }?
  • ?
  • void?SelectSort(int?iarr[],?int?len)????????????????????????//此函數是選擇,在無序的區間里面選擇一個最小的,然后把其置于有序區間的最后?
  • {?
  • ????int?loop1?=?0,loop2?=?0,?min?=?0,?pos?=?0;??????????????//min?保存的是最小的值,?pos保存的是最小值的下標位置?
  • ?
  • ????for(loop1?=?0;?loop1?<?len?;?++loop1)?
  • ????{???pos?=?loop1;?
  • ????????min?=?iarr[loop1];?
  • ????????for(loop2?=?loop1?+?1;?loop2?<?len?;?++loop2)?
  • ????????????if(min?>?iarr[loop2])?
  • ????????????{?min?=?iarr[loop2];????pos?=?loop2;}?
  • ?????
  • ????????iarr[pos]???=?iarr[loop1];????????????????????????????//此處賦值不可整錯?
  • ????????iarr[loop1]?=?min;?
  • ????}?
  • }?
  • ?
  • ?
  • ?
  • void?sift(int?iarr[],?int?low,?int?high)????//由于堆排序是要把堆化為大根堆或者小根堆,?此函數就是這個功能?
  • {?
  • ????int?loop1?=?low,?loop2?=?low?*2?,value?=?iarr[low];??
  • ?????
  • ????while(loop2?<=?high)?
  • ????{?
  • ????????if(loop2?<?high?&&?iarr[loop2]?<?iarr[loop2?+?1])?
  • ????????????++loop2;?
  • ????????if(value?<?iarr[loop2])????????????????//這里比較的是最初的iarr[low]的值,如果滿足條件的話,就把loop2繼續向下深入,因為是數組存儲,所以其子節點的下標為?loop2?*?2?
  • ????????{?
  • ????????????iarr[loop1]?=?iarr[loop2];?
  • ????????????loop1?=?loop2;??????????????????????//loop1?的作用相當于鏈表中?前驅?
  • ????????????loop2?=?loop2?*?2;?
  • ????????}else?break;?
  • ????}?
  • ????iarr[loop1]?=?value;?
  • }?
  • ?
  • void?HeapSort(int?iarr[],?int?len)??????????????????????????//堆排序的思想是,按堆的定義把數組調整為堆(創建初始堆,此后就是大根堆了),然后交換第一個元素與最后一個元素,這樣最后一個元素就是最大的值,然后對倒數第二個元素之前的元素進行堆排序,一直重復上個過程即可。?
  • {???????????????????????????????????????????????????//其中數組的開始是從1?開始的,第0個節點沒用?
  • ????int?mid?=?len?/?2,?loop1?=?0,?temp?=?0;?
  • ?????????????????????????????
  • ????for(;?mid?>?0;?--mid)???????//mid的指向是最后一個?枝節點,?對所有枝節點進行調整為堆。?
  • ????????sift(iarr,mid,len);?
  • ?????
  • ????for(loop1?=?len?;?loop1?>?1;?--loop1)?
  • ????{?
  • ????????temp?=?iarr[1];?
  • ????????iarr[1]?=?iarr[loop1];?
  • ????????iarr[loop1]?=?temp;?
  • ?
  • ????????sift(iarr,1,loop1?-?1);?????????????????????????//此處只執行一次是因為,除了根節點外都是滿足大(小)根堆的要求,所以只對根節點進行操作?
  • ????}?
  • }?
  • ?
  • ?
  • void?sift2(int?iarr[],?int?low,?int?high)?
  • {?
  • ????int?ivar1?=?low,?ivar2?=?low?*?2,?tempval?=?iarr[low];?
  • ?????????
  • ????while(ivar2?<=?high)?
  • ????{?
  • ????????if(ivar2?<?high?&&?iarr[ivar2]?>?iarr[ivar2?+?1])????//此時應該把最小的值放在根節點處?
  • ????????????++ivar2;?
  • ?????????
  • ????????if(tempval?>?iarr[ivar2])?
  • ????????{?
  • ????????????iarr[ivar1]?=?iarr[ivar2];?
  • ????????????ivar1?=?ivar2;?
  • ????????????ivar2?=?2?*?ivar2;?
  • ????????}else?break;?????????
  • ????}?
  • ????iarr[ivar1]?=?tempval;?
  • }?
  • ?
  • void?HeapSort2(int?iarr[],?int?len)?????????????//此是按照從大到小的順序排序,即樹是一個小根樹?
  • {?
  • ????int?loop1?=?0,?mid?=?len?/?2,?loop2?=?0,?temp?=?0;?
  • ?
  • ????for(;?mid?>?0;?--mid)?
  • ????????sift2(iarr,mid,len);?
  • ?
  • ????for(loop2?=?len?;?loop2?>?1;?--loop2)?
  • ????{?
  • ????????temp?=?iarr[1];?
  • ????????iarr[1]?=?iarr[loop2];?
  • ????????iarr[loop2]?=?temp;?
  • ?
  • ????????sift2(iarr,1,loop2?-?1);?
  • ????}?
  • }?
  • ?
  • bool?isSmallHeap(int?iarr[],?int?len)???????????????//判斷是否是一個小根堆,如果是的話,那么一個節點的左右子節點的值,都要比其雙親節點的值要大?
  • {???????????????????????????????????????????????????//要判斷數組的長度是為奇數還是偶數,?如果是偶數的話,那么最后一個枝節點就少一個右節點?
  • ????int???loop1?=?0,?mid?=?len?/?2;?
  • ????bool?isNOdd?=?false;?
  • ????if(len?%?2?==?0)??isNOdd?=?true;?
  • ????for(loop1?=?1;?loop1?<=?mid?;?++loop1)?
  • ????{?
  • ????????if(isNOdd?&&?loop1?==?mid)?
  • ????????{?
  • ????????????if(iarr[loop1]?<?iarr[2?*?loop1])?
  • ????????????????continue;?
  • ????????????else?return?false;?
  • ????????}?
  • ????????else?
  • ????????{?
  • ????????????if(iarr[loop1]?<?iarr[2?*?loop1]?&&?iarr[loop1]?<?iarr[2?*?loop1?+?1])?
  • ????????????????continue;?
  • ????????????else?return?false;?
  • ????????}?
  • ????}?
  • ?
  • ????return?true;?
  • }?
  • ?
  • void?BubbleSort(int?iarr[],?int?len)????????????????????????//冒泡排序,把相鄰的前后兩個元素成為有序?
  • {?
  • ????int?loop1?=?0,?loop2?=?0,?temp?=?0;?
  • ????bool?isExchange?=?false;????????????????????????????????//此變量表示是否交換了數據,?如果沒有交換,那么說明其順序是有序的不需要進行后面的排序操作,直接返回即可?
  • ????for(loop1?=?0;?loop1?<?len;?++loop1)?
  • ????{???isExchange?=?false;?
  • ????????for(loop2?=?len?-1?;?loop2?>?loop1?;?--loop2)?????????//此循環代表一次排序?
  • ????????{?
  • ????????????if(iarr[loop2]?<?iarr[loop2?-?1])?
  • ????????????{?????
  • ????????????????temp?=?iarr[loop2];?
  • ????????????????iarr[loop2]?=?iarr[loop2?-1];?
  • ????????????????iarr[loop2?-?1]?=?temp;?
  • ????????????????isExchange?=?true;?
  • ????????????}????????????
  • ????????}?
  • ????????if(!isExchange)?return?;?
  • ????}?
  • }?
  • ?
  • void?DBubble(int?iarr[],?int?len)???????????//雙向冒泡排序,?實際上是一個排序區間從兩邊向中間縮小的過程,?而單個冒泡排序則是從一邊向中間縮小的過程?
  • ?
  • {?
  • ????int?loop1?=?0,?mid?=?len?/?2,?loop2?=?0,temp?=?0;?
  • ?????
  • ????bool?isExchange?=?false;?
  • ????for(loop1?=?0;?loop1?<?mid;?)?
  • ????{???isExchange?=?false;?
  • ????????for(loop2?=?len?-1?-?loop1;?loop2?>?loop1;?--loop2)??????????//從后向前循環?
  • ????????{?
  • ????????????if(iarr[loop2]?<?iarr[loop2?-?1])?
  • ????????????{?
  • ????????????????temp?=?iarr[loop2];?
  • ????????????????iarr[loop2]?=?iarr[loop2?-?1];?
  • ????????????????iarr[loop2?-?1]?=?temp;?
  • ????????????????isExchange?=?true;?
  • ????????????}?
  • ????????}?
  • ????????++loop1;????????//此處++表明上一步驟后loop1已經存放了最終值,?所以不需要繼續對其進行排序?
  • ????????if(isExchange?&&?loop1?<?mid)???????????????????????????//表示當有交換值并且loop1還沒有達到臨界的時候?
  • ????????{?
  • ????????????for(loop2?=?loop1;?loop2?<?len?-?loop1;?++loop2)????????//此循環是從未達到最終值的左邊開始向右,但是條件是?loop2?<?len?-?loop1,表明后面排過序的元素不再比較?
  • ????????????{?
  • ????????????????if(iarr[loop2]?>?iarr[loop2?+?1])?
  • ????????????????{?
  • ????????????????????temp?=?iarr[loop2];?
  • ????????????????????iarr[loop2]?=?iarr[loop2?+?1];?
  • ????????????????????iarr[loop2?+?1]?=?temp;?
  • ????????????????}?
  • ????????????}?
  • ????????}else?return?;?
  • ????}?
  • }?
  • ?
  • ?
  • void?DBubble2(int?iarr[],?int?len)??????????//此循環同樣是一個雙向的冒泡排序,不同的是它的循環條件是exchange==1,這樣直到一趟排序中沒有交換的時候完成全部的排序?
  • {?
  • ????int?loop1?=?0,?ivar?=?0,?temp?=?0;?
  • ????int?exchange?=?1;?
  • ?
  • ????while(exchange?==?1)?
  • ????{?
  • ????????exchange?=?0;?
  • ????????for(loop1?=?len?-1?-ivar;?loop1?>?ivar;?--loop1)?
  • ????????{?
  • ????????????if(iarr[loop1]?<?iarr[loop1?-?1])?
  • ????????????{?
  • ????????????????temp?=?iarr[loop1];?
  • ????????????????iarr[loop1]?=?iarr[loop1?-?1];?
  • ????????????????iarr[loop1?-?1]?=?temp;?
  • ????????????????exchange?=?1;?
  • ????????????}?
  • ????????}?
  • ????????++ivar;?
  • ????????if(exchange?==?1)?
  • ????????for(loop1?=?ivar;?loop1?<?len?-?ivar;?++loop1)?
  • ????????{?
  • ????????????if(iarr[loop1]?>?iarr[loop1?+?1])?
  • ????????????{?
  • ????????????????temp?=?iarr[loop1];?
  • ????????????????iarr[loop1]?=?iarr[loop1?+?1];?
  • ????????????????iarr[loop1?+?1]?=?temp;?
  • ????????????}?
  • ????????}?
  • ????}?
  • }?
  • ?
  • ?
  • void?QuickSort(int?iarr[],?int?begin,?int?end)??????????//快速排序的思想是?先取出一個元素,然后從最后一個元素開始向左進行,直到找到一個比取得元素小的元素,把此元素放在取出元素的位置,?
  • {???????????????????????????????????????????????????????//然后從開始出向右進行,找到一個比取出元素大的元素,把其放在右邊取出的位置上,??如此循環直到向左的下標與向右的下標相遇,???此時這個下標就是剛開始取得元素的最終位置?
  • ????int?temp?=?iarr[begin],?loop1?=?begin,?loop2?=?end?-?1;?????????????????//快速排序與冒泡排序一樣,每一次循環都要把一個元素放在最終的位置上。?
  • //??????cout<<"loop1?:?"<<loop1<<"??loop2?:?"<<loop2<<"?begin?:?"<<begin<<"?end?:"<<end<<endl;?
  • ????if(begin?>=?end)?return;?
  • ??
  • ????while(loop1?<?loop2)?
  • ????{?
  • ????????while(loop1?<?loop2?&&?iarr[loop2]?>?temp)?
  • ????????????--loop2;?
  • ????????if(loop1?<?loop2)???????????????????????????//此處還需要有一個判斷,因為上一步的--loop2會使loop2?<?loop1?
  • ????????{???iarr[loop1]?=?iarr[loop2];?
  • ????????????++loop1;????????????????????????????????//此處進行++操作,因為原來的值不需要操作?
  • ????????}?
  • ??????????
  • ????????while(loop1?<?loop2?&&?iarr[loop1]?<?temp)?
  • ????????????++loop1;?
  • ????????if(loop1?<?loop2)?
  • ????????{?
  • ????????????iarr[loop2]?=?iarr[loop1];?
  • ????????????--loop2;?
  • ????????}?????????
  • ????}?
  • ?
  • ?cout<<"loop1?:?"<<loop1<<"??loop2?:?"<<loop2<<endl;?
  • ????iarr[loop1]?=?temp;?
  • ????QuickSort(iarr,begin,?loop1?);?
  • ????QuickSort(iarr,loop1?+?1,?end?);?
  • }?
  • ?
  • void?QuickSort2(int?iarr[],?int?begin,?int?end)?????????//此函數是快速排序的非遞歸算法,利用一個隊列保存每個區間?
  • {?
  • ????struct?
  • ????{?
  • ????????int?begin,?end;?
  • ????}st[MAXSIZE],temp;?
  • ????int?front?=?-1,?rear?=?-1,?ivar1?=?0,?ivar2?=?0,?tempval?=?0;?
  • ?
  • ????++rear;?
  • ????st[rear].begin?=?begin;?
  • ????st[rear].end???=?end;?
  • ?
  • ????while(front?<?rear)?
  • ????{?
  • ????????++front;?
  • ????????temp?=?st[front];?
  • ?
  • ????????ivar1?=?temp.begin;?
  • ????????ivar2?=?temp.end?-?1;?
  • ?
  • ????????if(ivar1?==?ivar2)?continue;????????????//當相遇的時候,也就是元素只有一個時,不執行操作?
  • ?
  • ????????tempval?=?iarr[ivar1];??????????????????//此句不能放在while循環內,因為ivar1的值在循環內是變化的?
  • ????????while(ivar1?<?ivar2)?
  • ????????{?
  • //??????????cout?<<"ivar1??"<<ivar1<<"?ivar2:?"<<ivar2<<endl;?
  • ????????????while(ivar2?>?ivar1?&&?iarr[ivar2]?>?tempval)?
  • ????????????????--ivar2;?
  • ????????????if(ivar2?>?ivar1)?
  • ????????????{?
  • ????????????????iarr[ivar1]?=?iarr[ivar2];?
  • ????????????????++ivar1;?
  • ????????????}?
  • //???????cout?<<"????iva?r1??"<<ivar1<<"?ivar2:?"<<ivar2<<endl;?
  • ????????????while(ivar2?>?ivar1?&&?iarr[ivar1]?<?tempval)?
  • ????????????????++ivar1;?
  • ????????????if(ivar2?>?ivar1)?
  • ????????????{?
  • ????????????????iarr[ivar2]?=?iarr[ivar1];?
  • ????????????????--ivar2;???
  • ????????????}?
  • //???????????cout?<<"?????????????ivar1??"<<ivar1<<"?ivar2:?"<<ivar2<<endl;?
  • ????????}?????
  • ????????cout<<"ivar1?:?"<<ivar1<<"??ivar2?:?"<<ivar2<<endl;??????????????//此處ivar2怎么會出現為?-1???因為這一句?ivar2?=?temp.end?-?1;????遞歸的不存在這個問題因為遞歸比較的是begin和end的值,?不是loop1?和?loop2的值?
  • ????????iarr[ivar1]?=?tempval;???????????????????//此句不能放在判斷語句內,因為這一句總是要執行的?
  • ????????if(ivar1?==?ivar2)??????????????????????//要注意此處的判斷條件,當ivar1?==?ivar2的時候才表明找到了一個元素的最終位置,最后一定有(ivar1?==?ivar2),此判斷要?
  • ????????{?
  • ????????????++rear;?
  • ????????????st[rear].begin?=?temp.begin;?
  • ????????????st[rear].end???=?ivar1;?
  • ????????????++rear;?
  • ????????????st[rear].begin?=?ivar1?+?1;?
  • ????????????st[rear].end?=?temp.end;?
  • ????????}?
  • ????}?
  • }?
  • /*?
  • void?Partition(int?iarr[],?int?begin,?int?end)?
  • {//?cout<<"begin?:?"<<begin<<"??end?:"<<end<<endl;?
  • ????int?temp?=?0,?ivar1?=?begin,?ivar2?=?end?,?mid?=?(ivar1?+?ivar2)?/?2?,?tempval?=?0;?
  • ????if(ivar1?>=?ivar2)?return;??
  • ????temp?=?iarr[mid];?
  • ?
  • ????while(ivar1?<?ivar2)?
  • ????{?
  • ????????while(ivar2?>?ivar1?&&?iarr[ivar2]?>=?temp)?
  • ????????????--ivar2;?
  • ????????while(ivar2?>?ivar1?&&?iarr[ivar1]?<=?temp)?
  • ????????????++ivar1;?
  • ????????if(ivar1?<?ivar2)?
  • ????????{?
  • ????????????tempval?=??iarr[ivar1];?
  • ????????????iarr[ivar1]?=?iarr[ivar2];?
  • ????????????iarr[ivar2]?=?tempval;?
  • ??????????????
  • ????????}?
  • ????}Disp(iarr,12);?
  • ??????
  • ????iarr[mid]?=?iarr[ivar1];?
  • ????iarr[ivar1]?=?temp;??
  • //???system("pause");?
  • ????Partition(iarr,begin,ivar1?-1);??
  • ????Partition(iarr,ivar1?+?1,?end);?
  • }?
  • ?
  • void?QuickSortX(int?iarr[],?int?begin,?int?end)?
  • {?
  • ????int?pos?=?0;?
  • ?????Partition(iarr,begin,?end);?
  • ????if(begin?<?pos?-1)?
  • ????????QuickSortX(iarr,begin,pos);?
  • ????if(pos?+?1?<?end?-?1)?
  • ????????QuickSortX(iarr,pos?+?1,end);?
  • }?
  • */?
  • ?
  • void?Merge(int?iarr[],?int?begin,?int?mid,?int?end)?????????????//歸并,把一個數組的前后兩個部分(有序)合并為一個有序的數組?
  • {?
  • ????int?iarr2[MAXSIZE],?loop1?=?0,?tempmid?=?0,?count?=?0;?
  • ?
  • ????for(loop1?=?0,?tempmid?=?mid?+?1;?loop1?<=?mid?&&?tempmid?<=?end;?)?
  • ????{?
  • ????????if(iarr[loop1]?>?iarr[tempmid])?
  • ????????{?
  • ????????????iarr2[count]?=?iarr[tempmid];?
  • ????????????++tempmid;?
  • ????????????++count;?
  • ????????}else?if(iarr[loop1]?<?iarr[tempmid])?
  • ????????{?
  • ????????????iarr2[count]?=?iarr[loop1];?
  • ????????????++loop1;?
  • ????????????++count;?
  • ????????}else?
  • ????????{?
  • ????????????iarr2[count]?=?iarr[loop1];?
  • ????????????++loop1;?
  • ????????????++count;?
  • ????????????iarr2[count]?=?iarr[tempmid];?
  • ????????????++tempmid;?
  • ????????????++count;?
  • ????????}?
  • ????}?
  • ????if(loop1?>?mid)?
  • ????{?
  • ????????while(tempmid?<=?end)?
  • ????????{?
  • ????????????iarr2[count]?=?iarr[tempmid];?
  • ????????????++count;?
  • ????????????++tempmid;?
  • ????????}?
  • ????}?
  • ????if(tempmid?>?end)?
  • ????{?
  • ????????while(loop1?<=??mid)?
  • ????????{?
  • ????????????iarr2[count]?=?iarr[loop1];?
  • ????????????++count;?
  • ????????????++loop1;?
  • ????????}?
  • ????}?
  • ????for(loop1?=?0;?loop1?<=?end;?++loop1)?
  • ????????iarr[loop1]?=?iarr2[loop1];?
  • }?
  • ?
  • void?MergePass(int?iarr[],?int?end,?int?length)??????//對一個數組進行一length長度的合并時候,應該判斷數組的長度是否為區間的偶數倍或者奇數倍?
  • {?
  • ????int?pos?=?0;?
  • ????for(pos?=?0;?pos?+?2?*?length?-1?<=?end;?pos?=?pos?+?2?*?length)?
  • ????????Merge(iarr,pos,pos?+?length?-?1,?pos?+?2?*?length?-?1);?
  • ????if(pos?+?length?-?1?<?end)?
  • ????????Merge(iarr,pos,?pos?+?length?-1?,?end);?
  • }??
  • ?
  • void?MergeSort(int?iarr[],?int?len)?????????//歸并排序,其思想是把數組先按照長度為1進行兩路排序,然后長度遞增?
  • {?
  • ????int?length?=?1;?
  • ????for(length?=?1;?length?<?len?;?length?*=?2)?
  • ????????MergePass(iarr,len,length);?
  • }?
  • ?
  • typedef?struct?rnode?
  • {?
  • ????char?data[MAXD];?
  • ????struct?rnode?*next;?
  • }RecType;?
  • ?
  • void?RadixSort(char?carr[][4],?int?len,?int?weishu)?????????????///基數排序?
  • {?
  • ????int?loop1?=?0,?num?=?0,count?=?0,?loop2?=?0;?
  • ????RecType?head[MAXSIZE],?*tail[MAXSIZE]?=?{NULL},?*temp?=?NULL;?
  • ??????
  • ????for(count?=?weishu?-1;count?>=?0;?--count)?
  • ????{??
  • ????????for(loop1?=?0;?loop1?<?MAXSIZE;?++loop1)?
  • ????????{?
  • ????????????tail[loop1]?=?NULL;?
  • ????????????head[loop1].next?=?NULL;?
  • ????????}?
  • ??????????
  • ????????for(loop1?=?0;?loop1?<?len;?++loop1)?????????????//按照位數位置上的數字添加到鏈表數組中,分配?
  • ????????{????
  • ????????????num?=?int(carr[loop1][count]?-?'0');??
  • ????????????temp?=?(RecType?*)malloc(sizeof(RecType));?
  • ????????????strcpy(temp->data,?carr[loop1]);?
  • ????????????temp->next?=?NULL;?
  • ????????????if(tail[num]?==?NULL)?
  • ????????????{???
  • ????????????????tail[num]?=?temp;?
  • ????????????????head[num].next?=?tail[num];?
  • ????????????}else?
  • ????????????{?
  • ????????????????temp->next?=?tail[num]->next;?
  • ????????????????tail[num]->next?=?temp;?
  • ????????????????tail[num]?=?temp;?
  • ????????????}?
  • ????????}?
  • ????????loop2?=?0;?
  • ????????for(loop1?=?0;?loop1?<?len;?++loop1)????????????//從鏈表中收集?
  • ????????{?
  • ????????????if(tail[loop1]?==?NULL)?
  • ????????????{cout<<"loop1??"<<loop1<<endl;?}?
  • ????????????else?
  • ????????????{?
  • ????????????????temp?=?head[loop1].next;?
  • ????????????????while(temp?!=?NULL)?
  • ????????????????{?????
  • ????????????????????strcpy(carr[loop2++],temp->data);????????????????????
  • ????????????????????temp?=?temp->next;?
  • ????????????????}?
  • ????????????}?
  • ????????}??
  • ??????
  • ????????for(loop1?=?0;?loop1?<len;?++loop1)?
  • ????????????cout<<carr[loop1]<<'?';?
  • ????????cout<<endl;??
  • ????}?
  • }?
  • ?

    轉載于:https://blog.51cto.com/saibro/1183655

    總結

    以上是生活随笔為你收集整理的排序相关算法-1的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。