用java写的一个快速排序算法源码(仅供参考)

public class QuickSortTest {
	public static void main(String[] args){
		int a[]={20, 3, 5, 18, 90, 100, 30, 21, 8, 19};
		quickSort(a, 0, a.length - 1);                        //指定从数组的哪个位置到哪个位置进行排序
		for(int i=0;i<a.length;i++){
			System.out.print(a[i]+" ");
		}
	}
	public static void quickSort(int a[], int start, int end){
		int temp = start;
		int i = start;
		int j = end;
		while(i != j){
			if(temp == i){
				for(;j > i; j--){
					if(a[temp] > a[j]){
						swap(a, temp, j);
						temp = j;
						i++;
						break;
					}
				}
			}
			if(temp == j){
				for(;i < j; i++){
					if(a[temp] < a[i]){
						swap(a, temp, i);
						temp = i;
						j--;
						break;
					}
				}
			}
		}
		
		/*
		 * 左、右两边重新递归调用该排序算法,请注意if语句里面的条件判断,
		 * 是start<temp-1而不是start<temp,如果写后面的就会报栈溢出的错误
		 */
		if(start < temp - 1){
			quickSort(a, start, temp - 1);
		}
		if(temp + 1 < end){
			quickSort(a, temp + 1, end);
		}
	}
	
	/*
	 * 将数组中的两个数进行交换
	 */
	private static void swap(int[] a, int t, int e) {
		int temp = a[t];
		a[t] = a[e];
		a[e] = temp;
	}
}


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值