快速排序算法

package Alg;

import java.util.Random;

public class QuickSort {
	public static void quickSort(int[] ary) {
		quickSort(ary, 0, ary.length - 1);
	}
	
	/**
	 * @param ary 数组 排序数组
	 * @param start 开始位置 ,0-base index
	 * @param end 结束位置,0-base index
	 */
	private static void quickSort(int[] ary, int start, int end) {
		if (start < end) {
			int p = partition(ary, start, end);
			quickSort(ary, start, p - 1);
			quickSort(ary, p + 1, end);
		}
	}
	
	private static int partition(int[] ary, int start, int end) {
		int pivot = start, i = start, j = start + 1;
		for (; j <= end; j++) {
			if (ary[j] < ary[pivot]) {
				i++;
				if (i == j)
					continue;
				int temp = ary[i];
				ary[i] = ary[j];
				ary[j] = temp;
			}
		}
		int temp = ary[pivot];
		ary[pivot] = ary[i];
		ary[i] = temp;
		return i;
	}
	
	public static void testCase1() {
		int length = 15;
//		int[] ary = new int[] {956,683,804,133,174,595,483,834,713,433,827,253,752,62,73};
		int[] ary = new int[length];
		java.util.Random rand = new Random();
		for (int i = 0; i < length; i++) {
			ary[i] = Math.abs(rand.nextInt()) % 1000;
		}
		
		for (int i = 0; i < length; i++)
			System.out.print(ary[i] + ",");
		
		System.out.println();
		
		quickSort(ary);
		
		for (int i = 0; i < length; i++)
			System.out.print(ary[i] + ",");
		
		System.out.println();
	}
}

  

转载于:https://www.cnblogs.com/northhurricane/p/3612574.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值