算法学习之排序(5)--快速排序

这篇博客详细介绍了快速排序算法,包括算法描述、时间复杂度、空间复杂度分析,并提供了C++实现代码,是理解与应用快速排序的好资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.算法描述

    快速排序使用分治法(Divide and conquer)策略来把一个序列(list)分为两个子序列(sub-lists)。
步骤为:
    1. 从数列中挑出一个元素,称为"基准"(pivot);
    2. 重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任一边)。在这个分区退出之后,该基准就处于数列的中间位置。这个称为分区(partition)操作。
    3. 递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序。

二.时间复杂度

1.最好情况:快速排序算法的最好情况出现在对数组进行划分时,每次都能够按照一定的比例划分为两个小的数组,在这种情况下,通过分析可以得出,最好情况下,快速排序的时间复杂度为O(nlogn)。
2.最差情况:快速排序算法最差的情况出现在对数组进行划分时,每次都将数组划分为一个空的小数组和一个有(n-1)个元素的数组,通过分析可以得出,最差情况下,快速排序的时间复杂度为O(n^2)。
3.平均情况:对于快速排序的平均情况下的分析比较复杂,留着以后再进行扩展,按照《算法导论》上面的分析,平均情况下,快速排序的时间复杂度为O(nlogn)。


三.空间复杂度

   虽然快速排序是in-place的排序算法,但是,快速排序存在一个递归调用的过程,每一次递归调用的过程,都要使用到栈的空间,因此,快速排序的空间复杂度并不是O(1)。在最好情况下和平均情况下,递归的次数为logn,因此,空间复杂度为O(logn)。在最差情况下,递归的次数为n,因此,空间复杂度为O(n)。


四.C++实现

#include <iostream>

#include "Sort.h"


template <class T>
void Print(T Ite, int Length)
{
    for (int Index = 0; Index < Length; Index++)
    {
        std::cout << (*(Ite + Index)) << " ";
    }
    std::cout << std::endl;
}

template <class T>
int Partion(T Ite, int Length)
{
    // top of stack
    T TOS = Ite;

    int MileStore = 0;


    int Last = (*(Ite + Length - 1));
    int Now = 0;

    //
    for (int Index= 0; Index < (Length -1); Index++)
    {
        Now = (*(Ite + Index));

        if (Now < Last)
        {
            (*(Ite + Index)) = (*TOS);
            (*TOS) = Now;

            //Print(Ite, Length);

            // update the TOS
            TOS++;
            MileStore++;
        }
    }


     // exchange the elements
     Now = (*TOS);
     (*TOS) = Last;
     (*(Ite + Length - 1)) = Now;


    return MileStore;

}


template <class T>
bool QuickSort(T Ite, int Length)
{
    if (Length < 1)
    {
        std::cout << "The Length of the array is empty." << std::endl;
        return false;
    }

    if (Length == 1)
    {
        std::cout << "The Length of the array is One." << std::endl;
        return true;
    }

    int Milestore = Partion(Ite, Length);
    std::cout << "The mile store is " << Milestore << std::endl;
    Print(Ite, Length);

    std::cout << "Left" << std::endl;
    QuickSort(Ite, Milestore);

    std::cout << "Right" << std::endl;
    QuickSort((Ite + Milestore + 1), (Length - 1 - Milestore));

    return true;
}

bool QuickSort(std::vector<int>& Array)
{
    return QuickSort(Array.begin(), Array.size());
}




五.参考文献

[1] Shaffer C A. Practical Introduction to Data Structure and Algorithm Analysis (C++ Edition)[J]. 2002.
[2] 科曼 and 金贵, 算法导论: 机械工业出版社, 2006.
[3] T. H. Cormen, C. E. Leiserson, R. L. Rivest, and C. Stein, "算法导论 (影印版)," ed: 北京: 高等教育出版社.
[4]http://zh.wikipedia.org/wiki/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F


版权所有,欢迎转载,转载请注明出处,谢谢微笑










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值