Java 三路快排

三路快速排序(3-Way QuickSort )是快速排序的优化版本,特别适用于处理包含大量重复元素的数组 。其核心思想是将数组划分为三个区域:小于基准值等于基准值大于基准值,从而减少不必要的递归和交换

三路快排原理

  1. 分区逻辑

    • 使用三个指针 lt(less than)、current(当前遍历位置)、gt(greater than)将数组划分为三部分:

      • [low, lt-1]小于基准值的元素

      • [lt, gt]等于基准值的元素

      • [gt+1, high]大于基准值的元素

    • 通过一次遍历,将元素分配到正确区域。

  2. 时间复杂度

    • 平均O(n log n)

    • 最坏 (大量重复元素时):O(n)(优于传统快排的 O(n²)

Java 实现代码

java 复制代码
public class ThreeWayQuickSort {

    public static void sort(int[] arr) {
        if (arr == null || arr.length <= 1) return;
        threeWayQuickSort(arr, 0, arr.length - 1);
    }

    private static void threeWayQuickSort(int[] arr, int low, int high) {
        if (low >= high) return;

        // 选择基准值(这里选第一个元素)
        int pivot = arr[low];
        int lt = low;      // 小于 pivot 的右边界
        int gt = high;     // 大于 pivot 的左边界
        int current = low; // 当前遍历指针

        while (current <= gt) {
            if (arr[current] < pivot) {
                swap(arr, lt, current);
                lt++;
                current++;
            } else if (arr[current] > pivot) {
                swap(arr, current, gt);
                gt--;
            } else {
                current++;
            }
        }

        // 递归处理小于和大于区域
        threeWayQuickSort(arr, low, lt - 1);
        threeWayQuickSort(arr, gt + 1, high);
    }

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

    public static void main(String[] args) {
        int[] arr = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
        sort(arr);
        System.out.println(Arrays.toString(arr)); 
        // 输出: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
    }
}

关键步骤解析

  1. 初始化指针

    • lt 指向数组起始位置(low),gt 指向数组末尾(high),currentlow 开始遍历。
  2. 遍历与交换

    • 如果 arr[current] < pivot:将 currentlt 处的元素交换,ltcurrent 均右移。

    • 如果 arr[current] > pivot:将 currentgt 处的元素交换,gt 左移(current 不移动,因为交换后的新元素需要再次检查)。

    • 如果 arr[current] == pivot:直接移动 current 指针。

  3. 递归处理子数组

    • [low, lt-1](小于区域)和 [gt+1, high](大于区域)递归排序,中间区域 [lt, gt] 已经有序。

示例流程

假设初始数组为 [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5],基准值 pivot=3

  1. 第一次分区后

    • 小于区域:[1, 1, 2]

    • 等于区域:[3, 3]

    • 大于区域:[4, 5, 9, 6, 5, 5]

  2. 递归排序小于区域 [1, 1, 2] 和大于区域 [4, 5, 9, 6, 5, 5]


优势与适用场景

  • 优势

    • 高效处理重复元素,避免传统快排的重复递归。

    • 减少元素交换次数。

  • 适用场景

    • 数组中存在大量重复元素(如日志数据、用户行为数据)。

    • 需要稳定排序但允许非稳定实现的情况。

相关推荐
AI_Gump31 分钟前
【AI阅读】20250717阅读输入
java·spring boot·spring
找不到、了1 小时前
Java排序算法之<插入排序>
java·算法·排序算法
设计师小聂!1 小时前
力扣热题100----------53最大子数组和
java·数据结构·算法·leetcode
笠码1 小时前
JVM Java虚拟机
java·开发语言·jvm·垃圾回收
thginWalker2 小时前
八股文之JVM
java
Cyanto2 小时前
MyBatis-Plus高效开发实战
java·开发语言·数据库
qhd吴飞2 小时前
mybatis 差异更新法
java·前端·mybatis
YuTaoShao2 小时前
【LeetCode 热题 100】51. N 皇后——回溯
java·算法·leetcode·职场和发展
null不是我干的3 小时前
基于黑马教程——微服务架构解析(一)
java·微服务·架构
Bonnie_12153 小时前
04-netty基础-Reactor三种模型
java·nio·jetty