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

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)

不稳定

相关推荐
2401_8582861115 分钟前
125.【C语言】数据结构之归并排序递归解法
c语言·开发语言·数据结构·算法·排序算法·归并排序
钱彬 (Qian Bin)28 分钟前
一文掌握Qt Quick数字图像处理项目开发(基于Qt 6.9 C++和QML,代码开源)
c++·开源·qml·qt quick·qt6.9·数字图像处理项目·美观界面
guygg881 小时前
基于matlab的FIR滤波器
开发语言·算法·matlab
双叶8361 小时前
(C++)学生管理系统(正式版)(map数组的应用)(string应用)(引用)(文件储存的应用)(C++教学)(C++项目)
c语言·开发语言·数据结构·c++
源代码•宸1 小时前
C++高频知识点(二)
开发语言·c++·经验分享
ysh98881 小时前
PP-OCR:一款实用的超轻量级OCR系统
算法
遇雪长安2 小时前
差分定位技术:原理、分类与应用场景
算法·分类·数据挖掘·rtk·差分定位
数通Dinner2 小时前
RSTP 拓扑收敛机制
网络·网络协议·tcp/ip·算法·信息与通信
jyan_敬言3 小时前
【C++】string类(二)相关接口介绍及其使用
android·开发语言·c++·青少年编程·visual studio
liulilittle3 小时前
SNIProxy 轻量级匿名CDN代理架构与实现
开发语言·网络·c++·网关·架构·cdn·通信