【AU】交换排序之 快速排序

本文深入解析了快速排序算法,介绍了其平均最优时间复杂度O(nlog2n),空间复杂度在递归调用下为O(log2n)。探讨了其不稳定的特性,并适合于顺序存储的数据结构。提供了一段Java实现的代码示例。

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

快速排序

快速排序的基本思想是基于 分治法 的。

快速排序是 所有内部排序算法中平均性能最优的排序算法

时间复杂度

最好情况:O(n log2n)
最坏情况:O(n2)
平均情况:O(n log2n)

空间复杂度

快速排序是递归调用的,因此需要一个递归栈,来保存每次递归的有用的信息。
最好情况:O(log2n)
最坏情况:O(n)
平均情况:O(log2n)

稳定性

左右区间元素的相对位置会变化,不稳定!

适用性

顺序存储(链式存储)

代码

import java.util.Scanner;

public class Quick {

	public static void sort(int []list,int low,int high) {
		if(low<high) {
			int position=partition(list, low, high);
			sort(list,low,position-1);
			sort(list,position+1,high);
		}
	}
	public static int partition(int []list,int low,int high) {
		int pivot=list[low];
		while(low<high) {
			while(low<high && list[high]>=pivot) {
				high--;
			}
			list[low]=list[high];
			while(low<high && list[low]<=pivot) {
				low++;
			}
			list[high]=list[low];
		}
		list[low]=pivot;
		return low;
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] list = new int[n];
		for (int i = 0; i < n; i++) {
			list[i] = sc.nextInt();
		}
		int low=0;
		int high=list.length-1;
		sort(list,low,high);
		for(int i=0;i<n;i++) {
			System.out.print(list[i]+"  ");
		}
		sc.close();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

莫余

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值