快速排序

#include<stdio.h>
 
void swap(int *a, int *b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}
 
int partition(int a[], int low, int high)
{
	int privotKey = a[low];					//privotKey为基准元素			
	while(low < high)
	{								  
		while(low < high  && a[high] >= privotKey) 
		{
			--high;  
		}
		swap(&a[low], &a[high]);
		while(low < high  && a[low] <= privotKey ) 
		{
			++low;
		}
		swap(&a[low], &a[high]);
	}
	return low;
}
 
 
void quickSort(int a[], int low, int high)
{
	if(low < high)
	{
		int privotLoc = partition(a,  low,  high); //将表分为两部分
		quickSort(a,  low,  privotLoc -1);		
		quickSort(a,   privotLoc + 1, high);	
	}
}
 
int main(){
	int a[10] = {3,1,5,7,2,4,9,6,10,8};
	quickSort(a,0,9);
	for(int i=0;i<10;i++)
	{
		printf("%d ",a[i]);
	}
	while(1);
}

思路:先取个基准元素将表分为两部分,一部分为小于基准元素,另一部分大于基准元素,再将这两部分重复该过程直道排序完成。

优缺点:性能平稳,但是不稳定。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值