自己写的快排-已验证

//参考自:http://developer.51cto.com/art/201403/430986.htm
#include <cassert>
#include <stdlib.h>


/*
//自己写的快排
2018年7月20日星期五 18:07
找出标准数
以其为标准进行交换
至到相遇或超过
同一个index||相差一个


-----------------------
first是左侧数组索引
second是右侧的数组索引
B2S: 是否是Big to Small
-----------------------
*/
void qsort(const size_t leftIndex, const size_t rightIndex, const bool B2S, int array[])
{
    if (leftIndex >= rightIndex)
    {
        return;
    }
    size_t standard_value = array[leftIndex];
    //找到标准数新的位置:分水岭的位置
    size_t standard_new_index = 0;
    //分成两部分
    /*
    左边找一个合适的index
    右遍找一个合适的index

    */
    for (;;)
    {
        size_t tmpLIndex = leftIndex + 1;
        size_t tmpRIndex = rightIndex;
    SORT_INDEX_EQUAL:
        if (tmpLIndex == tmpRIndex)//已经移动到一起了
        {
            /*和标准比较大小
            1   2//
            1   0//就放在其

            */
            size_t mid_value = array[tmpLIndex];
            if (B2S)
            {
                if (standard_value < mid_value)
                {
                    //标准数需要放在mid右侧

                    array[leftIndex] = array[tmpLIndex];
                    array[tmpLIndex] = standard_value;
                    standard_new_index = tmpLIndex;

                }
                else
                {
                    //标准数需要放在mid左侧

                    standard_new_index = tmpLIndex - 1;
                    array[leftIndex] = array[standard_new_index];
                    array[tmpLIndex - 1] = standard_value;

                }
            }
            else
            {
                if (standard_value < mid_value)
                {
                    //标准数需要放在mid左侧
                    standard_new_index = tmpLIndex - 1;
                    array[leftIndex] = array[standard_new_index];
                    array[tmpLIndex - 1] = standard_value;
                }
                else
                {
                    //标准数需要放在mid右侧
                    array[leftIndex] = array[tmpLIndex];
                    array[tmpLIndex] = standard_value;
                    standard_new_index = tmpLIndex;
                }
            }
            break;
        }

        for (;; ++tmpLIndex)
        {
            if (tmpLIndex == tmpRIndex)
            {
                goto SORT_INDEX_EQUAL;
            }
            if (B2S)
            {
                if (array[tmpLIndex] < standard_value)
                {
                    break;
                }
            }
            else
            {
                if (array[tmpLIndex] > standard_value)
                {
                    break;
                }
            }
            continue;
        }

        for (;; --tmpRIndex)
        {
            if (tmpLIndex == tmpRIndex)
            {
                goto SORT_INDEX_EQUAL;
            }
            if (B2S)
            {
                if (array[tmpRIndex] > standard_value)
                {
                    break;
                }
            }
            else
            {
                if (array[tmpRIndex] < standard_value)
                {
                    break;
                }
            }
            continue;
        }

        //交换
        if (B2S)
        {
            if (array[tmpLIndex] < array[tmpRIndex])
            {
                size_t tmp = array[tmpRIndex];
                array[tmpRIndex] = array[tmpLIndex];
                array[tmpLIndex] = tmp;
            }
        }
        else
        {
            if (array[tmpLIndex] > array[tmpRIndex])
            {
                size_t tmp = array[tmpRIndex];
                array[tmpRIndex] = array[tmpLIndex];
                array[tmpLIndex] = tmp;
            }
        }
        if (1 == (tmpRIndex - tmpLIndex))//已经相临 且经过前面的交换
        {
            /*
            似乎很简单
            */
            array[leftIndex] = array[tmpLIndex];
            array[tmpLIndex] = standard_value;
            standard_new_index = tmpLIndex;
            break;
        }

    }

    //防止边界
    if (standard_new_index != leftIndex)
    {
        qsort(leftIndex, standard_new_index - 1, B2S, array);
    }
    if (standard_new_index != rightIndex)
    {
        qsort(standard_new_index + 1, rightIndex, B2S, array);
    }
    return;
}
void test_qsort(int first[], int seoncd[], size_t number)
{
    for (size_t i = 0; i < number; ++i)
    {
        if (first[i] != seoncd[i])
        {
            assert(false);
        }
    }
}

void qsort(int arr[],size_t length ,bool B2S)
{
    if(0== length)
    {
        return;
    }
    qsort(0, length -1, B2S,arr);
}

int main()
{
    {
        int array1[] = { 0,1,2,3,4,5 };
        int array1OK[] = { 5,4,3,2,1,0 };
        int sz1 = _countof(array1);
        //qsort(0, sz1 - 1, true, array1);
        qsort(array1,sz1, true);
        test_qsort(array1, array1OK, sz1);
    }

    {
        int array1[] = { 5,1,2,3,4,5 };
        int array1OK[] = { 5,5,4,3,2,1 };
        int sz1 = _countof(array1);
        qsort(array1, sz1, true);
        test_qsort(array1, array1OK, sz1);
    }

    {
        int array1[] = { 5,1,2,3,4,5,6,6,6 };
        int array1OK[] = { 6,6,6,5,5,4,3,2,1 };
        int sz1 = _countof(array1);
        qsort(array1, sz1, true);
        test_qsort(array1, array1OK, sz1);
    }

    {
        int array1[] = { 5,1,2 };
        int array1OK[] = { 5,2,1 };
        int sz1 = _countof(array1);
        qsort(array1, sz1, true);
        test_qsort(array1, array1OK, sz1);
    }

    {
        int array1[] = { 5,2,1 };
        int array1OK[] = { 5,2,1 };
        int sz1 = _countof(array1);
        qsort(array1, sz1, true);
        test_qsort(array1, array1OK, sz1);
    }
    /测试由 高到低
    {
        int array1[] = { 0,1,2,3,4,5 };
        int array1OK[] = { 0,1,2,3,4,5 };
        int sz1 = _countof(array1);
        qsort(array1, sz1, false);
        test_qsort(array1, array1OK, sz1);
    }

    {
        int array1[] = { 5,1,2,3,4,5 };
        int array1OK[] = { 1,2,3,4,5,5 };
        int sz1 = _countof(array1);
        qsort(array1, sz1, false);
        test_qsort(array1, array1OK, sz1);
    }

    {
        int array1[] = { 5,1,2,3,4,5,6,6,6 };
        int array1OK[] = { 1,2,3,4,5,5,6,6,6 };
        int sz1 = _countof(array1);
        qsort(array1, sz1, false);
        test_qsort(array1, array1OK, sz1);
    }

    {
        int array1[] = { 5,1,2 };
        int array1OK[] = { 1,2,5 };
        int sz1 = _countof(array1);
        qsort(array1, sz1, false);
        test_qsort(array1, array1OK, sz1);
    }

    {
        int array1[] = { 5,2,1 };
        int array1OK[] = { 1,2,5 };
        int sz1 = _countof(array1);
        qsort(array1, sz1, false);
        test_qsort(array1, array1OK, sz1);
    }
    printf("All Test Pass");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值