Ai
1 Star 4 Fork 5

Shapper/C Data Structure

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
quick_sort.h 728 Bytes
一键复制 编辑 原始数据 按行查看 历史
Shapper 提交于 2023-04-07 17:23 +08:00 . add quick sort
//
// Quick Sort
// Created by Win10 on 2023/4/7.
//
#ifndef C_DATA_STRUCTURE_QUICK_SORT_H
#define C_DATA_STRUCTURE_QUICK_SORT_H
#include <algorithm>
template<typename T>
//整数或浮点数皆可使用
void quick_sort(T a[], int l, int r) {
if (l >= r) return;
int i = l, j = r + 1;
int x = a[l];
while (true) {
//从左向右找比x大的值
while (a[++i] < x)
if (i == r) break;
//从右向左找比x小的值
while (a[--j] > x)
if (j == l) break;
if (i >= j) break;
//交换i,j对应的值
std::swap(a[i], a[j]);
}
//中枢值与j对应值交换
std::swap(a[l], a[j]);
quick_sort(a, l, j - 1);
quick_sort(a, j + 1, r);
}
#endif //C_DATA_STRUCTURE_QUICK_SORT_H
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C/C++
1
https://gitee.com/cloudcan/c-data-structure.git
git@gitee.com:cloudcan/c-data-structure.git
cloudcan
c-data-structure
C Data Structure
master

搜索帮助