C语言实现快速排序算法

#include <stdio.h>

void swap(int* a, int* b) {

int t = *a;

*a = *b;

*b = t;

}

int partition (int arr\[\], int low, int high) {

int pivot = arrhigh;

int i = (low - 1);

for (int j = low; j <= high - 1; j++) {

if (arrj < pivot) {

i++;

swap(&arri, &arrj);

}

}

swap(&arri + 1, &arrhigh);

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);

}

}

void printArray(int arr\[\], int size) {

for (int i = 0; i < size; i++)

printf("%d ", arri);

printf("\n");

}

int main() {

int arr\[\] = {10, 7, 8, 9, 1, 5};

int n = sizeof(arr)/sizeof(arr0);

quickSort(arr, 0, n-1);

printf("Sorted array: \n");

printArray(arr, n);

return 0;

}

相关推荐
6Hzlia7 分钟前
【Classic 150 刷题计划】 LeetCode 228. 汇总区间 | C++ 双游标区间扫描与 to_string 规范
c++·算法·leetcode
SuperByteMaster28 分钟前
configUSE_TIME_SLICING和configUSE_PREEMPTION
c语言
周杰偷奶茶29 分钟前
【C 语言】分支、循环和 goto 语句(附实战)
c语言
玖玥拾1 小时前
LeetCode 383 赎金信
算法·leetcode
维克兜率天1 小时前
5.3 宏观因子:抬头看天
笔记·python·深度学习·算法·量化
LuminousCPP1 小时前
数据结构-排序(五):计数排序与排序专题阶段总结|从外部归并到线性排序,再看算法边界与选型
c语言·数据结构·笔记·算法·排序算法
流云枫1 小时前
003-上下文窗口:物理限制、KV Cache 与工程应对策略
算法
山下梅子酒2251 小时前
洛谷-入门-B2046
c语言
盟道科技1 小时前
小程序订阅消息大规模投递实践:频控令牌池、Redis限流与失败补偿设计
分布式·算法·小程序
工具派1 小时前
视频场景检测是怎么找到切镜头位置的?三种思路与实测
算法·计算机视觉·视频