排序之快速排序

快速排序是一种高效的排序算法,它采用分治法的思想,有“分而治之”的特点。

有三个过程:

一、分解,把大问题拆解到很小的问题。

二、每个小问题都能够有方法解决。

三、合并,把小问题各个答案合并


package com;
//快速排序,默认第一个元素为主元
public class Problem1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i = 0 ;
		int a[] = {5,4,9,8,7,6,0,1,3,2};
		int len = a.length;
		sortIntegers2(a);
		for(i=0;i<len;i++)
			System.out.println(a[i]+" ");
	}
	
	public static void sortIntegers2(int[] A) {
        // write your code here
		sort(A,0,A.length-1);
	}
	//快速排序
	private static void sort(int[] array, int low, int high) {
		// TODO Auto-generated method stub
		int i,j;
		int index;
		if(low >= high)
			return;
		i = low;
		j = high;
		index = array[i]; //默认第一个为主元
		while(i < j){
			while(i < j && array[j] >= index)
				j--;
			if(i<j)
				array[i++] = array[j];
			while(i<j && array[i] < index)
				i++;
			if(i < j)
				array[j--] = array[i];
		}
		array[i] = index ; 
		sort(array,low,i-1);
		sort(array,i+1,high);
		
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值