Java排序方法总结(持续更新中......)

2020-01-20  本文已影响0人  大数据ZRL

快速排序

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
}
上一篇 下一篇

猜你喜欢

热点阅读