java实现快速排序

        
/**
 * 
 * @author think
 *	快速排序
 *	时间复杂度o(n*logn); 
 */
public class QuickSort {
	
	public static int exe(int[]a,int start,int end)
	{
		int base = a[end];//取末尾元素为基准
		while(start < end)
		{
			while(start < end && a[start] <= base)//向右排查
				start++;
			if(start < end)//将比基准小的元素放在左侧
			{
				int temp = a[start];
				a[start] = a[end];
				a[end] = temp;
				end--;
			}
			while(start < end && a[end] >= base)//向左排查
				end--;
			if(start < end)//将比基准大的元素放在右侧
			{
				int temp = a[start];
				a[start] = a[end];
				a[end] = temp;
				start++;
			}
		}
		return end;//返回分界
	}

	public static void quickSort(int[]a,int start,int end)
	{
		if(start > end)
			return;
		else
		{
			int position = exe(a,start,end);
			quickSort(a,start,position-1);
			quickSort(a,position+1,end);
		}
	}
	
	public static void main(String[] args) {
		int[] array=new int[]{1,2,3,30,7,8,44,10,19,16};
		quickSort(array,0,array.length-1);
		for(int i=0;i<array.length;i++)
		{
			System.out.print(array[i]+" ");
		}
	}
}

       算法思想就是在一个混乱序列中找到一个基准b,进行一趟遍历,将比基准b小的元素放在其左侧,反之放在其右侧。再以b为分界将原序列分为两个子序列,分别找到各自新的基准进行递归操作,递归的结束标志就是细分的数组仅含一个元素,此时就完成了对整个序列的排序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值