快速排序

本文详细介绍了快速排序算法的基本原理,包括其最优、最坏及平均情况下的时间复杂度,并通过具体的C++代码实现了快速排序的过程。文章还指出快速排序是一种不稳定的排序算法。

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

在最优的情况下,快速排序算法的时间复杂度为O(nlongn)

在最坏的情况下,快速排序算法的时间复杂度为O(n*n)

平均的时间复杂度为O(nlongn)

在最优的情况下,快速排序算法的时间复杂度为O(longn)

在最坏的情况下,快速排序算法的时间复杂度为O(n)

平均的时间复杂度为O(longn)

快速排序是一种不稳定的排序算法

#include<iostream>

using namespace std;

int Partition(int *num,int low,int high){
	int pivot=num[low];
	while(low<high){
		while(low<high && num[high]>=pivot)
			high--;
		num[low]=num[high];
		while(low<high && num[low]<=pivot)
			low++;
		num[high]=num[low];
	}
	num[low]=pivot;
	return low;
}


//快速排序
void QuitSort(int *num, int low, int high){
	if(low<high){
		int pivot=Partition(num,low,high);//划分 
		QuitSort(num, low, pivot-1);
		QuitSort(num, pivot+1, high);
	}
} 


int main(){
	int num[10]={4,0,2,3,1,8,5,4,12,6};
	QuitSort(num,0,10);
	for(int i=0; i<10; i++)
		cout<<num[i]<<" ";
	cout<<endl;
	return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值