快速排序的精美写法

227 阅读1分钟
class QUICKSORT{
	public void quickSort(int[] array, int leftBound, int rightBound){
		if(leftBound >= rightBound) return;
		int middle = patition(int[] array, int leftBound, int rightBound);
		quickSort(array, leftBound, middle - 1);
		quickSort(array, middle + 1, rightBound);	
	}
	public int patition(int[] array, int leftBound, int rightBound){
		int pivot = array[rightBound];
		int left = leftBound, right = rightBound - 1;
		while(left <= right){
			while(left <= right && array[left] <= pivot) left++;
			while(left <= right && array[right] > pivot) right--;
			if(left < right) swap(array, left, right);
		}
		swap(array, left, rightBound);
	}
	public void swap(int[] array, int i, int j){
		int temp = array[i];
		array[i] = array[j];
		array[j] = temp;
	}
	return left;
}