快速排序
private static void quickSort(Integer[] arr, int left, int right) {
if (right <= left) {
return;
}
int head = left;
int tail = right;
while (left < right) {
//find the element smaller than head element from right side
while (arr[right] > arr[head] && right > left) {
right--;
}
//find the element bigger than head element from left side
while (arr[left] <= arr[head] && right > left) {
left++;
}
if (right > left) {
swap(arr, left, right);
}
}
swap(arr, head, right);//like: [[smlaller ele...],head ele,[bigger ele...]]
quickSort(arr, 0, right - 1);//recursion
quickSort(arr, right + 1, tail);//recursion
}
网友评论