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

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)

不稳定

相关推荐
Pniubi14 分钟前
01背包类问题-力扣416.分割等和子集
算法·leetcode·职场和发展
eggwyw11 小时前
C++ union 的一种妙用法
开发语言·c++
罗西的思考11 小时前
【轻量化复现大模型 / KIMI K3】MTP / EAGLE‑3 模块交互式演示
人工智能·深度学习·算法
ShineWinsu12 小时前
对于Qt:QHBoxLayout常见属性以及方法的解析
c++·qt·面试·gui·客户端·布局管理器·水平布局器
小七在进步14 小时前
string的实现(1)
c++·笔记·visual studio
kyle~16 小时前
模型预测控制(MPC)
算法·机器人·控制·传感器
敲上瘾16 小时前
Qt核心机制:信号与槽详解
开发语言·c++·qt·ui·qt6.3
牢姐与蒯17 小时前
cpp算法之哈希
c++·算法·哈希算法
开开心心就好18 小时前
两张表格数据对比工具,不用写函数也能查
算法·spring·智能手机·pdf·电脑·intellij-idea·音视频
lcj251118 小时前
[C++] C++11
开发语言·c++·笔记