快速排序 C语言实现

问题:快速排序

算法:

代码:

cpp 复制代码
#include<stdio.h>

int nums[10] = {4,2,8,3,1,6,5,0,10,9};

void QuickSort(int *nums,int left,int right){
    if(left >= right)       return; // 递归出口
    int pivot = nums[left];
    int low = left,high = right;
    while(low < high){
        while(low < high && nums[high] >= pivot)        high--; // 从后往前找到第一个小于pivot的
        nums[low] = nums[high]; // 放到low的位置(nums[low]的值不会消失,因为已经提前存入pivot中了)
        while(low < high && nums[low] <= pivot) low++; // 从前往后找到第一个大于pivot的
        nums[high] = nums[low]; // 放到high的位置(刚才空出来了)
    }
    nums[low] = pivot; // 把pivot放到现在low的位置
    QuickSort(nums,left,low - 1); // 递归前半部分
    QuickSort(nums,low + 1,right); // 递归后半部分
    return ;
}

int main(){
    QuickSort(nums,0,9);
    for(int i = 0;i<10;i++) printf("%d ",nums[i]);
    putchar('\n');
    return 0;
}
相关推荐
长安er27 分钟前
LeetCode215/347/295 堆相关理论与题目
java·数据结构·算法·leetcode·
元亓亓亓35 分钟前
LeetCode热题100--62. 不同路径--中等
算法·leetcode·职场和发展
小白菜又菜1 小时前
Leetcode 1925. Count Square Sum Triples
算法·leetcode
粉红色回忆1 小时前
用链表实现了简单版本的malloc/free函数
数据结构·c++
登山人在路上2 小时前
Nginx三种会话保持算法对比
算法·哈希算法·散列表
写代码的小球2 小时前
C++计算器(学生版)
c++·算法
AI科技星2 小时前
张祥前统一场论宇宙大统一方程的求导验证
服务器·人工智能·科技·线性代数·算法·生活
予枫的编程笔记3 小时前
Redis 核心数据结构深度解密:从基础命令到源码架构
java·数据结构·数据库·redis·缓存·架构
wadesir3 小时前
掌握Rust并发数据结构(从零开始构建线程安全的多线程应用)
数据结构·安全·rust
Fuly10243 小时前
大模型剪枝(Pruning)技术简介
算法·机器学习·剪枝