【c语言】快速排序

简介: 【c语言】快速排序

快速排序是一种使用分治思想的排序算法,它的时间复杂度为O(nlogn)。以下是两种C语言实现快速排序的方法:

方法一:使用递归实现快速排序

#include <stdio.h>
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}
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);
    }
}
int main() {
    int arr[] = {10, 7, 8, 9, 1, 5};
    int n = sizeof(arr)/sizeof(arr[0]);
    quickSort(arr, 0, n-1);
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    return 0;
}
代码注释:
swap函数:交换两个数的值。
partition函数:选择末尾元素作为基准值,将小于基准值的元素移动到数组左端,大于基准值的元素移动到数组右端。
quickSort函数:使用递归的方式将数组分成左右两部分并进行排序。
main函数:调用quickSort函数对数组进行排序。

方法二:使用栈实现快速排序

#include <stdio.h>
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}
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) {
    int stack[high-low+1];
    int top = -1;
    stack[++top] = low;
    stack[++top] = high;
    while (top >= 0) {
        high = stack[top--];
        low = stack[top--];
        int pi = partition(arr, low, high);
        if (pi-1 > low) {
            stack[++top] = low;
            stack[++top] = pi-1;
        }
        if (pi+1 < high) {
            stack[++top] = pi+1;
            stack[++top] = high;
        }
    }
}
int main() {
    int arr[] = {10, 7, 8, 9, 1, 5};
    int n = sizeof(arr)/sizeof(arr[0]);
    quickSort(arr, 0, n-1);
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    return 0;
}
代码注释:
swap函数:交换两个数的值。
partition函数:选择末尾元素作为基准值,将小于基准值的元素移动到数组左端,大于基准值的元素移动到数组右端。
quickSort函数:使用栈的方式将子数组的边界入栈,并循环进行排序。
main函数:调用quickSort函数对数组进行排序。
目录
相关文章
|
3天前
|
C语言
【C语言/数据结构】排序(快速排序及多种优化|递归及非递归版本)
【C语言/数据结构】排序(快速排序及多种优化|递归及非递归版本)
11 0
|
3天前
|
算法 C语言
C语言之冒泡排序、快速排序法、希尔排序法
C语言之冒泡排序、快速排序法、希尔排序法
|
7月前
|
存储 算法
玩转快速排序(C语言版)
玩转快速排序(C语言版)
52 0
|
10月前
|
算法 搜索推荐 C语言
c语言写快速排序代码
c语言写快速排序代码
|
5月前
|
算法 搜索推荐 编译器
一文带你学透快排(快速排序C语言版)
一文带你学透快排(快速排序C语言版)
|
5月前
|
存储 算法 C语言
【数据结构】深入浅出理解快速排序背后的原理 以及 版本优化【万字详解】(C语言实现)
【数据结构】深入浅出理解快速排序背后的原理 以及 版本优化【万字详解】(C语言实现)
61 0
|
7月前
|
算法 C语言
C语言---数据结构实验---查找算法的实现---实现给定数组的快速排序
C语言---数据结构实验---查找算法的实现---实现给定数组的快速排序
|
7月前
|
算法 C语言
【C语言】快速排序
【C语言】快速排序
153 0
|
8月前
|
搜索推荐 算法 C语言
【数据结构】—从冒泡排序丝滑过度快速排序(含C语言实现)
【数据结构】—从冒泡排序丝滑过度快速排序(含C语言实现)
【数据结构】—从冒泡排序丝滑过度快速排序(含C语言实现)
|
9月前
|
搜索推荐 算法 程序员
C语言快速排序降序实现
快速排序是一种常用的排序算法,其灵活性和高效性使其成为程序员们喜爱的排序方式之一。在这篇文章中,我们将探讨如何使用C语言来实现快速排序算法,并实现一个降序排序的例子。
118 0