码迷,mamicode.com
首页 > 编程语言 > 详细

快速排序

时间:2019-11-25 09:26:34      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:printf   print   array   pivot   class   for   div   quicksort   size   

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("\n");
    system("pause");
}

 

快速排序

标签:printf   print   array   pivot   class   for   div   quicksort   size   

原文地址:https://www.cnblogs.com/predator-wang/p/11925654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!