快速排序(数据结构)

1. 前言:

这两种排序经常使用,且在算法题中经常遇见。

这里我们简单分析讨论一下。

1. 快速排序

平均时间复杂度:O(nlogn)

最坏时间复杂度: O(n^2)

1.1. 左右向中遍历:

  1. 取最右侧4为基础元素,取出后,4的位置为null。
  2. 从有空的另一侧开始遍历比较,如果左侧3的元素小于基准元素4,则不变化,反之则把左侧元素交换到右侧,左侧为null。
  3. 然后从右侧开始遍历。
  4. 反复,直到low,high指针相遇。

这种方式易于理解却不便于书写,需要反复判断是向左遍历还是向右遍历。

csharp 复制代码
public class QuickSortVariant {

    public static void main(String[] args) {
        int[] arr = {1, 3, 6, 2, 4};
        quickSort(arr, 0, arr.length - 1);
        for (int i : arr) {
            System.out.print(i + " ");
        }
    }

    public static void quickSort(int[] arr, int low, int high) {
        if (low < high) {
            int pivotIndex = partition(arr, low, high);
            quickSort(arr, low, pivotIndex - 1);
            quickSort(arr, pivotIndex + 1, high);
        }
    }

    public static int partition(int[] arr, int low, int high) {
        int pivot = arr[high]; // 选择最右侧元素作为基准
        int left = low;
        int right = high - 1;

        while (left < right) {
            // 从左侧寻找大于基准的元素
            while (arr[left] <= pivot && left < right) {
                left++;
            }
            // 从右侧寻找小于基准的元素
            while (arr[right] >= pivot && left < right) {
                right--;
            }
            // 交换左右找到的元素
            swap(arr, left, right);
        }

        // 如果最后左指针指向的元素大于基准,与基准交换
        if (arr[left] >= pivot) {
            swap(arr, left, high);
        } else {
            left++;
        }

        return left; // 返回基准元素的最终位置
    }

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

1.1. 单侧遍历:

这种遍历理解麻烦点,书写却简单点。

  1. i 指针指向小于base元素的结尾。这里我指向了数组前的一个元素,这不是错误,而是一个巧妙的设计。
  2. 如果 j 指针指向的元素小于base,则 i 指针扩充一个元素,即i++,然后swap(i,j)一般情况下二者是重合的。
  3. 如果遇见大于base的指针,则j++,i 不变,等效与记录了这个大元素指针的位置,再遇到小的元素可以通过swap(i,j)交换,这时,则真正起作用。
java 复制代码
public class Solution {
    public static void main(String[] args) {
        int []arr = new int[]{1,3,6,2,4};
        sort(arr,0,4);
        System.out.println(Arrays.toString(arr));
    }

    public  static void sort(int[]arr,int low, int high){
        int baseIndex = quickSort(arr,low,high);
        quickSort(arr,low,baseIndex-1);
        quickSort(arr,baseIndex + 1,high);
    }
    public static int quickSort(int[]arr,int low, int high){
        int base = arr[high];
        int i = low - 1;
        for(int j = low; j < arr.length - 1; j++){
            if(arr[j] < base){
                i++;
                swap(arr,i,j);
            }
        }
        swap(arr,high,i + 1);
        return i + 1;



    }
    public static void swap(int[]arr,int index0, int index1){
        int tem = arr[index0];
        arr[index0] = arr[index1];
        arr[index1] = tem;
    }

}
相关推荐
默默无名的大学生1 小时前
数据结构—顺序表
数据结构·windows
BillKu1 小时前
推荐 Eclipse Temurin 的 OpenJDK
java·ide·eclipse
Morri31 小时前
[Java恶补day53] 45. 跳跃游戏Ⅱ
java·算法·leetcode
悟能不能悟1 小时前
eclipse怎么把项目设为web
java·eclipse
乂爻yiyao1 小时前
java 代理模式实现
java·开发语言·代理模式
2301_770373732 小时前
Java集合
java·开发语言
Jared_devin2 小时前
二叉树算法题—— [蓝桥杯 2019 省 AB] 完全二叉树的权值
数据结构·c++·算法·职场和发展·蓝桥杯
哈喽姥爷2 小时前
Spring Boot---自动配置原理和自定义Starter
java·spring boot·后端·自定义starter·自动配置原理
AI 嗯啦3 小时前
数据结构深度解析:二叉树的基本原理
数据结构·算法
hai_qin3 小时前
十三,数据结构-树
数据结构·c++