快速排序(代码及其分析)

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;

 const  int N = 100010;
 
    int a[N];
    
int Partition(int q[],int low,int high)
{
    int pivot=q[low];
    while(low<high)
    {
    while(low<high&&q[high]>=pivot) high--;
    q[low]=q[high];
    while(low<high&&q[low]<=pivot) low++;
    q[high]=q[low];
    }
    q[low]=pivot;
    return low;
}


void QuickSort(int q[],int low,int high)
{
    if(low<high)
    {
    int pos=Partition(q,low,high);
    QuickSort(q,low,pos-1);
    QuickSort(q,pos+1,high);
    }
   
}


int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    QuickSort(a,0,n-1);
    for(int i=0;i<n;i++)
    {
        printf("%d ",a[i]);
    }
    
}

最好时间复杂度O

空间复杂度O

平均时间复杂度O****

最坏时间复杂度O(n*2)

不稳定

相关推荐
mit6.8242 小时前
[实现Rpc] 通信类抽象层 | function | using | 解耦合设计思想
c++·网络协议·rpc
laimaxgg3 小时前
Qt常用控件之单选按钮QRadioButton
开发语言·c++·qt·ui·qt5
尼尔森系4 小时前
排序与算法:希尔排序
c语言·算法·排序算法
AC使者4 小时前
A. C05.L08.贪心算法入门
算法·贪心算法
冠位观测者4 小时前
【Leetcode 每日一题】624. 数组列表中的最大距离
数据结构·算法·leetcode
sushang~5 小时前
leetcode203.移除链表元素
数据结构·链表
yadanuof5 小时前
leetcode hot100 滑动窗口&子串
算法·leetcode
可爱de艺艺5 小时前
Go入门之函数
算法
ox00805 小时前
C++ 设计模式-命令模式
c++·设计模式·命令模式
武乐乐~5 小时前
欢乐力扣:旋转图像
算法·leetcode·职场和发展