Java之快速排序

快排1.0

public class QuickSort {
    public static void main(String[] args) {
        int[] arr = new int[]{6, 1, 5, 10, 25, 6, 7, 7,-1};
        quickSort1(arr);
        Arrays.stream(arr).forEach(item -> System.out.print(item + " "));
    }


    /**
     * 快排1.0
     *
     * @param arr
     */
    public static void quickSort1(int[] arr) {
        quickSort1(arr, 0, arr.length - 1);

    }

    public static void quickSort1(int[] arr, int left, int right) {
        if (left < right) {
            int[] partition = partition(arr, left, right);
            quickSort1(arr, left, partition[0]-1);
            quickSort1(arr, partition[0] + 1, right);
        }
    }

    /**
     * 快排划分
     *
     * @param arr
     * @param left
     * @param right
     * @return
     */
    public static int[] partition(int[] arr, int left, int right) {
        int lPoint = left - 1;
        int point = left;
        int target = arr[right];
        while (point <= right) {
            if (arr[point] <= target) {
                swap(arr, ++lPoint, point++);
            } else {
                point++;
            }
        }
        return new int[]{lPoint};
    }


    public static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

快排2.0

在快排1.0的基础上,修改划分方法。将相同的元素放置在数组中间,相当于一次性可能排好多个元素(这些元素相等)。

public class QuickSort2 {
    public static void main(String[] args) {
        int[] arr = new int[]{6, 1, 5, 10, 25, 6, 7, 7,-1};
        quickSort2(arr);
        Arrays.stream(arr).forEach(item -> System.out.print(item + " "));
    }

    public static void quickSort2(int[] arr) {
        quickSort2(arr, 0, arr.length - 1);
    }

    public static void quickSort2(int[] arr, int left, int right) {
        if (left < right) {
            int[] partition = partition(arr, left, right);
            quickSort2(arr, left, partition[0]);
            quickSort2(arr, partition[1], right);
        }
    }


    public static int[] partition(int[] arr, int left, int right) {
        int lPoint = left - 1;
        int rPoint = right + 1;
        int point = left;
        int target = arr[right];
        while (point < rPoint) {
            if (arr[point] < target) {
                swap(arr, ++lPoint, point++);
            } else if (arr[point] > target) {
                swap(arr, --rPoint, point);
            } else {
                point++;
            }
        }
        return new int[]{lPoint, rPoint};
    }

    public static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

快排3.0

说明: 无论快排1.0,还是快排2.0,时间复杂度都是O(n^2)。因为取排序数组的最后一个数作为基准,可能存在最坏的情况。例如:1,2,3,4,5,6,7,8,9。所以快排3.0,就是在快排2.0的基础上,随机选取一个数作为基准。根据数学平均推定,时间复杂度为 O(nlogn)

public class QuickSort3 {

    public static void main(String[] args) {
        int[] arr = new int[]{6, 1, 5, 10, 25, 6, 7, 7, -1};
        quickSort(arr);
        Arrays.stream(arr).forEach(item -> System.out.print(item + " "));
    }

    public static void quickSort(int[] arr) {
        quickSort(arr, 0, arr.length - 1);
    }

    public static void quickSort(int[] arr, int left, int right) {
        if (left < right) {
            int[] partition = partition(arr, left, right);
            quickSort(arr, left, partition[0]);
            quickSort(arr, partition[1], right);
        }
    }

    public static int[] partition(int[] arr, int left, int right) {
        int lPoint = left - 1;
        int rPoint = right + 1;
        int point = left;
        //随机取下标
        int target = arr[left + (int) Math.random() * (right - left)];
        while (point < rPoint) {
            if (arr[point] < target) {
                swap(arr, ++lPoint, point++);
            } else if (arr[point] > target) {
                swap(arr, --rPoint, point);
            } else {
                point++;
            }
        }
        return new int[]{lPoint, rPoint};
    }

    public static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值