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;
}
快速排序的精美写法
最新推荐文章于 2022-06-12 22:38:34 发布