快速排序


public class Qiicc {

	static int a[] = { 5, 4, 1, 2, 4, 5, 7, 8, 9 };

	public static void main(String args[])
	{
		qSort(0, a.length-1);
		
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + " ");
		}
	}
	
	public static void qSort(int low, int high) {
		if (low < high) {
			int pivot = partition(low, high);
			qSort(low, pivot - 1);
			qSort(pivot + 1, high);
		}

	}

	private static int partition(int low, int high) {

		int pivotKey = a[low];
		while (low < high) {
			while (low < high && a[high] >= pivotKey)
				high--;
			a[low] = a[high];

			while (low < high && a[low] <= pivotKey)
				low++;
			a[high] = a[low];

		}

		a[low] = pivotKey;

		return low;
	}

}

你可能感兴趣的:(快速排序)