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

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)

不稳定

相关推荐
apcipot_rain6 小时前
C++转Java日记(04)
java·开发语言·c++
NEFU_Jyj6 小时前
牛客周赛R168
算法
WL学习笔记8 小时前
vector模拟实现
c++
蜗牛一步步往上爬11 小时前
第十六章 C++语言在深度学习中的应用
开发语言·c++·深度学习
likexuec13 小时前
数据结构(2)
数据结构
Navigator_Z14 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
wo_jack14 小时前
C++ 入门笔记(十六):第16课 vector 模拟实现十问十答 —— 从三指针到迭代器失效
c++·后端·面试·stl·vector
D e re k ZH15 小时前
C++ 标准库算法全景指南
数据结构·c++·算法
冻柠檬飞冰走茶15 小时前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划