快速排序
Posted predator-wang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了快速排序相关的知识,希望对你有一定的参考价值。
void QuickSort(int nLow, int nHigh) { int s = nLow; int e = nHigh; int nPivot = nLow; if (s >= e) { return; } while (1) { while (g_szArray[nLow] <= g_szArray[nPivot] && nLow <= e) { nLow++; } while (g_szArray[nHigh] > g_szArray[nPivot] && nHigh >= s) { nHigh--; } if (nLow > nHigh) { break; } else { g_szArray[nLow] = g_szArray[nLow] + g_szArray[nHigh]; g_szArray[nHigh] = g_szArray[nLow] - g_szArray[nHigh]; g_szArray[nLow] = g_szArray[nLow] - g_szArray[nHigh]; nLow++; nHigh--; } } /*g_szArray[nHigh] = g_szArray[nPivot] + g_szArray[nHigh]; g_szArray[nPivot] = g_szArray[nHigh] - g_szArray[nPivot]; g_szArray[nHigh] = g_szArray[nHigh] - g_szArray[nPivot];*/ int nTmp = g_szArray[nHigh]; g_szArray[nHigh] = g_szArray[nPivot]; g_szArray[nPivot] = nTmp; QuickSort(nLow, e); QuickSort(s, nLow - 1); } void main() { int nLen = sizeof(g_szArray) / sizeof(g_szArray[0]); QuickSort(0, nLen - 1); for (int i = 0; i < nLen; i++) { printf("%d ", g_szArray[i]); } printf(" "); system("pause"); }
以上是关于快速排序的主要内容,如果未能解决你的问题,请参考以下文章