c++ 经典的快速排序QuickSort完整代码片 清泛编译 2020/09/19 c++快速排序算法实现,经典的一种写法,来自Github,原文有个bug,本文已修正代码如下: #include <iostream> void printArray(int *array, int n) { for (int i = 0; i < n; ++i) std::cout << array[i] << std::endl; } void quickSort(int *array, int low, int high) { int i = low; int j = high; int pivot = array[(i + j) / 2]; int temp; while (i <= j) { while (array[i] < pivot) i++; while (array[j] > pivot) j--; if (i <= j) { temp = array[i]; array[i] = array[j]; array[j] = temp; i++; j--; } } if (j > low) quickSort(array, low, j); if (i < high) quickSort(array, i, high); } int main() { int array[] = {95, 45, 48, 98, 1, 485, 65, 478, 1, 2325}; int n = sizeof(array)/sizeof(array[0]); std::cout << "Before Quick Sort :" << std::endl; printArray(array, n); quickSort(array, 0, n-1); std::cout << "After Quick Sort :" << std::endl; printArray(array, n); return (0); }C++CopyCompile & Run 代码直接可运行,可直接用于实际项目开发。 Github地址:QuickSort.cpp c++ quicksort sort 对我有用(3)100%没啥用(0)0% 分享到: 上一篇:百度分享不支持https的解决方案 下一篇:Git 工具 - 子模块(submodule):一个仓库包含另一个仓库 98%的人还看了 · XunSearch(讯搜)的使用教程步骤(2016-05-31) · wandbox:C++在线编译项目源码编译及原理剖析(2021-01-10) · Linux C++静态链接protobuf库异常中止(2021-07-04)