自己写的快排-已验证

//参考自: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: 好的,我可以帮你一个快排程序,下面是一个简单的实现: def quick_sort(arr): if len(arr) <= 1: return arr else: pivot = arr[0] left = [x for x in arr[1:] if x < pivot] right = [x for x in arr[1:] if x >= pivot] return quick_sort(left) + [pivot] + quick_sort(right) ### 回答2: 快速排序(Quick Sort)是一种常用的排序算法,它基于分治的思想,通过递归地将问题划分为两个子问题,并对子问题进行排序最终将结果合并的方式进行排序。 以下是使用Python编的快速排序程序: ```python def quick_sort(arr): if len(arr) <= 1: return arr pivot = arr[len(arr) // 2] # 选择基准值 left = [x for x in arr if x < pivot] # 将小于基准值的元素放在左边 middle = [x for x in arr if x == pivot] # 将等于基准值的元素放在中间 right = [x for x in arr if x > pivot] # 将大于基准值的元素放在右边 return quick_sort(left) + middle + quick_sort(right) # 递归排序左右两部分 # 测试 arr = [6, 3, 9, 2, 5, 1] sorted_arr = quick_sort(arr) print(sorted_arr) ``` 上述代码定义了一个名为`quick_sort`的函数,接受一个列表作为输入,并返回排序后的列表。 在`quick_sort`函数中,我们首先判断列表长度是否小于等于1,若是,则直接返回列表本身。否则,选择一个基准值(这里使用中间值作为基准值),将小于、等于和大于基准值的元素分别放在左、中、右三个列表中。然后,对左右两部分分别进行递归调用`quick_sort`函数,最后将左边、中间和右边三部分合并,即得到排序后的结果。 在测试部分,我定义了一个无序列表`arr`,并调用`quick_sort`函数对其进行排序,将结果赋值给`sorted_arr`变量,并打印结果。 执行以上代码,将输出经过快速排序后的有序列表`[1, 2, 3, 5, 6, 9]`。 ### 回答3: 快速排序(Quicksort)是一种常用的排序算法,其核心思想是通过选择一个基准元素,将序列分割为两个子序列,然后分别对子序列进行递归排序,最后将两个有序部分合并起来。 下面是用Python编的快速排序程序: ```python def quicksort(arr): if len(arr) <= 1: return arr else: pivot = arr[0] # 选择第一个元素作为基准 less = [x for x in arr[1:] if x <= pivot] # 小于等于基准的子序列 greater = [x for x in arr[1:] if x > pivot] # 大于基准的子序列 return quicksort(less) + [pivot] + quicksort(greater) # 递归排序并合并结果 # 测试 arr = [5, 2, 9, 1, 7, 6, 3] sorted_arr = quicksort(arr) print(sorted_arr) ``` 在这个程序中,我们定义了一个名为`quicksort`的函数,它采用一个数组作为参数,并返回排序后的数组。 在函数内部,我们首先判断数组的长度。如果长度小于等于1,说明数组已经是有序的,直接返回该数组。否则,我们选择第一个元素作为基准(pivot),然后使用列表推导式分别生成小于等于基准的子序列`less`和大于基准的子序列`greater`。然后,我们对`less`和`greater`进行递归调用`quicksort`,将两个有序的子序列和基准进行合并,并返回合并后的结果。 最后,我们通过测试数据来验证快速排序的正确性并输出排序结果。 希望这个快排程序能帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值