基于visual Studio2013解决算法导论之011快排改良
生活随笔
收集整理的這篇文章主要介紹了
基于visual Studio2013解决算法导论之011快排改良
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目
快排改良
解決代碼及點(diǎn)評(píng)
#include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <time.h>#define K 3 void PrintArr(int *pnArr, int nLen) {for (int i = 0; i < nLen; i++){printf("%d ", pnArr[i]);}printf("\n"); }void Swap(int *p1, int *p2) {int nTmp = *p1;*p1 = *p2;*p2 = nTmp; }int Partition(int *pnArr, int nLeft, int nRight) {int nKey = nRight;int i = nLeft - 1;for (int j = nLeft; j < nRight; j++){if (pnArr[j] < pnArr[nKey]){i++;Swap(&pnArr[i], &pnArr[j]);}}Swap(&pnArr[i+1], &pnArr[nRight]);return i + 1; } int RandomPartition(int *pnArr, int nLeft, int nRight) {srand(time(NULL));int n1 = rand()%(nRight - nLeft + 1) + nLeft;int n2 = rand()%(nRight - nLeft + 1) + nLeft;int n3 = rand()%(nRight - nLeft + 1) + nLeft;int nKey = (n1+n2+n3) / 3;Swap(&pnArr[nKey], &pnArr[nRight]);return Partition(pnArr, nLeft, nRight); }void InsertSort(int *pnArr, int nLeft, int nRight) {for (int i = nLeft + 1; i <= nRight; i++){int nTmp = pnArr[i];int j;for (j = i; j > nLeft && nTmp <pnArr[j-1]; j--){pnArr[j] = pnArr[j-1];}pnArr[j] = nTmp;} } void QuickSort(int *pnArr, int nLeft, int nRight) {if (nLeft < nRight){if (nRight - nLeft > K){int nTmpPos = RandomPartition(pnArr, nLeft, nRight);QuickSort(pnArr, nLeft, nTmpPos - 1);QuickSort(pnArr, nTmpPos + 1, nRight);}else{InsertSort(pnArr, nLeft, nRight);}} } int main() {int nArr[10] = {42,1,3,2,16,9,10,14,8,17}; PrintArr(nArr, 10);QuickSort(nArr, 0,9);PrintArr(nArr, 10);system("pause");return 0; }代碼下載及其運(yùn)行
代碼下載地址:http://download.csdn.net/detail/yincheng01/6858815
解壓密碼:c.itcast.cn
下載代碼并解壓后,用VC2013打開interview.sln,并設(shè)置對(duì)應(yīng)的啟動(dòng)項(xiàng)目后,點(diǎn)擊運(yùn)行即可,具體步驟如下:
1)設(shè)置啟動(dòng)項(xiàng)目:右鍵點(diǎn)擊解決方案,在彈出菜單中選擇“設(shè)置啟動(dòng)項(xiàng)目”
2)在下拉框中選擇相應(yīng)項(xiàng)目,項(xiàng)目名和博客編號(hào)一致
3)點(diǎn)擊“本地Windows調(diào)試器”運(yùn)行
程序運(yùn)行結(jié)果
轉(zhuǎn)載于:https://www.cnblogs.com/niulanshan/p/6175057.html
總結(jié)
以上是生活随笔為你收集整理的基于visual Studio2013解决算法导论之011快排改良的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Flash怎么制作一片树叶飘落动画
- 下一篇: Eclipse 配置SSH 详解