快速排序算法的实现

void swap_int1(int &i1, int &i2)
{
    i1 = i1 + i2;
    i2 = i1 - i2;
    i1 = i1 - i2;
}

int partion(int *ptrA, int lenA, int beg, int end)
{
    int index = rand()%(end-beg)+beg;
    swap_int1(ptrA[end], ptrA[index]);
    int smallPos = beg - 1;
    int i = beg;
    for(; i<end; ++i)
    {
        if(ptrA[i] < ptrA[end])
        {
            ++smallPos;
            if(i != smallPos)
                swap_int1(ptrA[i], ptrA[smallPos]);
        }
    }
    ++smallPos;
    swap_int1(ptrA[smallPos], ptrA[end]);
    return smallPos;
}


void QuickSort(int *ptrA, int lenA, int beg, int end)
{
    if(beg < end)
    {
        int index = partion(ptrA, lenA, beg, end);
        if(index >beg)
            QuickSort(ptrA, lenA, beg, index-1);
        if(index < end)
            QuickSort(ptrA, lenA, index+1, end);
    }
}

 

排序算法就像是数字信号里边的傅里叶变换一样基础,在此继续温习一遍。

posted @ 2016-05-23 21:48  BestWangJie  阅读(170)  评论(0编辑  收藏  举报