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

快速排序


算法原理

快速排序(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);
    }
}

总结

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

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

相关推荐
luck_bor9 小时前
IO流知识点笔记
java·开发语言·笔记
包子BI大数据9 小时前
3.openclaw小龙虾简单版安装教程
人工智能·python·ai
程序大视界9 小时前
【Python系列课程】Pandas(四):数据统计与排序——describe、sort_values、sample
开发语言·python·pandas
妄想出头的工业炼药师9 小时前
LVIO鲁棒
算法·开源
aini_lovee9 小时前
MATLAB 图像修复 — 偏微分方程方法
算法
XGeFei9 小时前
【Fastapi学习笔记(4)】—— JsonScheme与数据验证、错误响应格式、正则表达式
学习·fastapi
Cthy_hy9 小时前
Python算法竞赛:排列组合核心用法
开发语言·python·算法
大圣编程9 小时前
面向对象深度理解
java·开发语言·算法
爱喝水的鱼丶10 小时前
SAP-ABAP:SAP 简单报表输出开发系列(共6篇) 第四篇:SAP 报表异常处理机制:数据校验与消息提示规范落地
开发语言·数据库·学习·算法·sap·abap
影寂ldy10 小时前
C# const 常量 / readonly 只读 / static readonly
java·开发语言·c#