美文网首页
Java排序方法总结(持续更新中......)

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

作者: 大数据ZRL | 来源:发表于2020-01-20 17:52 被阅读0次

快速排序

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
}

相关文章

网友评论

      本文标题:Java排序方法总结(持续更新中......)

      本文链接:https://www.haomeiwen.com/subject/mrajzctx.html