每天学习一个小算法:快速排序

快速排序


算法原理

快速排序(Quick Sort)是一种分治法(Divide and Conquer)排序算法。其基本思想是:选择一个元素作为基准值(pivot),将数组分区为两个子数组------比基准值小的元素和比基准值大的元素,然后递归地对这两个子数组进行排序。

快速排序的关键操作是分区(partition):重新排列数组,使得所有小于基准值的元素都移到基准值左边,所有大于基准值的元素都移到基准值右边。基准值在排序完成后就处于最终的正确位置


一、适用场景?

快速排序是实际应用中最常用的排序算法之一,特别适合:

  • 大数据集的排序,平均性能远优于O(n²)算法
  • 内存受限的场景(原地排序,空间开销小)
  • 对速度要求高的系统级应用
  • 不需要稳定性的排序场景
  • 大多数编程语言标准库的默认排序实现

快速排序的平均时间复杂度为O(n log n),在随机数据下表现最优。通过随机选择基准值或三数取中法,可以有效避免最坏情况的发生。

二、复杂度分析

1.时间复杂度

  • 最佳情况:O(n log n)
  • 平均情况:O(n log n)
  • 最坏情况:O(n²)(已排序数组)

2.空间复杂度

  • 递归栈:O(log n)(最佳)
  • 最坏:O(n)
  • 原地排序算法

三、代码实现

1、Python实现

python 复制代码
# 快速排序 Python 实现
def partition(arr, low, high):
    pivot = arr[high]
    i = low - 1
    for j in range(low, high):
        if arr[j] <= pivot:
            i = i + 1
            arr[i], arr[j] = arr[j], arr[i]
    arr[i + 1], arr[high] = arr[high], arr[i + 1]
    return i + 1

def quickSort(arr, low, high):
    if low < high:
        pi = partition(arr, low, high)
        quickSort(arr, low, pi - 1)
        quickSort(arr, pi + 1, high)

# 调用方式: quickSort(arr, 0, len(arr) - 1)

2、Java实现

java 复制代码
// 快速排序 Java 实现
public class QuickSort {
    void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
    
    int partition(int[] arr, int low, int high) {
        int pivot = arr[high];
        int i = (low - 1);
        for (int j = low; j < high; j++) {
            if (arr[j] < pivot) {
                i++;
                swap(arr, i, j);
            }
        }
        swap(arr, i + 1, high);
        return i + 1;
    }
    
    void quickSort(int[] arr, int low, int high) {
        if (low < high) {
            int pi = partition(arr, low, high);
            quickSort(arr, low, pi - 1);
            quickSort(arr, pi + 1, high);
        }
    }
}

3、C语言实现

c 复制代码
// 快速排序 C 语言实现
void swap(int* a, int* b) {
    int t = *a;
    *a = *b;
    *b = t;
}

// 分区函数
int partition(int arr[], int low, int high) {
    int pivot = arr[high];
    int i = (low - 1);
    
    for (int j = low; j <= high - 1; j++) {
        if (arr[j] < pivot) {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return (i + 1);
}

// 快速排序主函数
void quickSort(int arr[], int low, int high) {
    if (low < high) {
        int pi = partition(arr, low, high);
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}

总结

记录自己的快乐学习日志,也祝贺观看到这的小伙伴早日学有所成,财富自由💰💰。

记得点赞👍、收藏👋呀!!!

相关推荐
计算机编程-吉哥24 分钟前
YOLO26 vs YOLO11 vs YOLOv8:深度学习咖啡果实成熟度分割系统【计算机毕业设计选题推荐】
人工智能·python·深度学习·yolo·django·毕业设计
彧azz35 分钟前
算法设计与分析:贪心与动态规划
数据结构·学习·算法·贪心算法·动态规划
泡海椒36 分钟前
PDF 表格样式优化:jquick-pdf 边框、圆角、背景色
java·开发语言·pdf
大衛說1 小时前
11 · 异常处理与日志
python
auto_go1 小时前
Python 实战指南(7)——账本动不动就崩?先把“魔法字符串”和“裸报错”干掉
python
Beyond_System|系统之外1 小时前
【学编程】Python基础编程题100道(21-60)
开发语言·python·算法
根目录下的猫2 小时前
RK3588适配的轻量级AI模型推荐
人工智能·后端·python·目标检测
景熙55232 小时前
15.Java 8 Stream 流入门到实战
java·开发语言·数据结构
wno7042 小时前
Spring Security权限控制
java·python·spring