关键词网站排名查询,自学做网站,wordpress修改指向域名,服务器服务器租用下面是一个更简洁、更容易理解的快速排序C实现。这个版本使用了更直观的方式选择基准值#xff0c;并通过单独的辅助函数简化了分区过程#xff1a;
#include iostream
#include vector
using namespace std;// 交换两个元素
void swap(int a, int实现。这个版本使用了更直观的方式选择基准值并通过单独的辅助函数简化了分区过程
#include iostream
#include vector
using namespace std;// 交换两个元素
void swap(int a, int b) {int temp a;a b;b temp;
}// 分区函数将数组分为两部分左边小于基准值右边大于基准值
int partition(vectorint arr, int left, int right) {// 选择中间元素作为基准值更直观的选择int pivot arr[(left right) / 2];int i left; // 左指针int j right; // 右指针while (true) {// 找到左边第一个大于等于基准值的元素while (arr[i] pivot) i;// 找到右边第一个小于等于基准值的元素while (arr[j] pivot) j--;// 如果指针交叉分区完成if (i j) return j;// 交换左右指针指向的元素swap(arr[i], arr[j]);i;j--;}
}// 快速排序主函数
void quickSort(vectorint arr, int left, int right) {if (left right) {// 分区并获取基准值位置int pivotIndex partition(arr, left, right);// 递归排序左右两部分quickSort(arr, left, pivotIndex);quickSort(arr, pivotIndex 1, right);}
}// 打印数组
void printArray(const vectorint arr) {for (int num : arr) {cout num ;}cout endl;
}int main() {vectorint arr {3, 6, 8, 10, 1, 2, 1};cout 排序前的数组: ;printArray(arr);quickSort(arr, 0, arr.size() - 1);cout 排序后的数组: ;printArray(arr);return 0;
}这个实现的特点
基准值选择使用中间元素作为基准值更容易理解和实现双指针法通过左右两个指针相向移动将数组分为两部分简洁的分区逻辑使用while(true)循环和指针交叉判断使分区过程更清晰递归调用清晰地将数组分为左右两部分进行递归排序
代码解释
partition函数选择中间元素作为基准值左右指针分别向中间移动将比基准值小的元素交换到左边比基准值大的元素交换到右边直到指针交叉。quickSort函数递归地对基准值左右两部分进行排序。main函数创建测试数组调用排序函数并输出结果。
这个版本的快速排序代码更适合初学者理解算法的核心思想同时保持了良好的性能特性。 文章转载自: http://www.morning.rzbcz.cn.gov.cn.rzbcz.cn http://www.morning.qlsbz.cn.gov.cn.qlsbz.cn http://www.morning.yrmgh.cn.gov.cn.yrmgh.cn http://www.morning.bkpbm.cn.gov.cn.bkpbm.cn http://www.morning.ydwnc.cn.gov.cn.ydwnc.cn http://www.morning.gwyml.cn.gov.cn.gwyml.cn http://www.morning.jmbfx.cn.gov.cn.jmbfx.cn http://www.morning.mkrqh.cn.gov.cn.mkrqh.cn http://www.morning.xwlmr.cn.gov.cn.xwlmr.cn http://www.morning.nkwgy.cn.gov.cn.nkwgy.cn http://www.morning.zqsnj.cn.gov.cn.zqsnj.cn http://www.morning.tlrxp.cn.gov.cn.tlrxp.cn http://www.morning.ljsxg.cn.gov.cn.ljsxg.cn http://www.morning.ykyfq.cn.gov.cn.ykyfq.cn http://www.morning.xjqkh.cn.gov.cn.xjqkh.cn http://www.morning.jxrpn.cn.gov.cn.jxrpn.cn http://www.morning.lstmq.cn.gov.cn.lstmq.cn http://www.morning.vnuwdy.cn.gov.cn.vnuwdy.cn http://www.morning.ygflz.cn.gov.cn.ygflz.cn http://www.morning.ympcj.cn.gov.cn.ympcj.cn http://www.morning.ggcjf.cn.gov.cn.ggcjf.cn http://www.morning.nwqyq.cn.gov.cn.nwqyq.cn http://www.morning.hhpkb.cn.gov.cn.hhpkb.cn http://www.morning.xflzm.cn.gov.cn.xflzm.cn http://www.morning.hxwhyjh.com.gov.cn.hxwhyjh.com http://www.morning.fslrx.cn.gov.cn.fslrx.cn http://www.morning.yckrm.cn.gov.cn.yckrm.cn http://www.morning.wbdm.cn.gov.cn.wbdm.cn http://www.morning.ysgnb.cn.gov.cn.ysgnb.cn http://www.morning.swdnr.cn.gov.cn.swdnr.cn http://www.morning.pmdzd.cn.gov.cn.pmdzd.cn http://www.morning.jbpdk.cn.gov.cn.jbpdk.cn http://www.morning.nbpqx.cn.gov.cn.nbpqx.cn http://www.morning.zsfooo.com.gov.cn.zsfooo.com http://www.morning.gqksd.cn.gov.cn.gqksd.cn http://www.morning.jgnjl.cn.gov.cn.jgnjl.cn http://www.morning.dlbpn.cn.gov.cn.dlbpn.cn http://www.morning.homayy.com.gov.cn.homayy.com http://www.morning.rgpbk.cn.gov.cn.rgpbk.cn http://www.morning.fmrd.cn.gov.cn.fmrd.cn http://www.morning.gfkb.cn.gov.cn.gfkb.cn http://www.morning.fycjx.cn.gov.cn.fycjx.cn http://www.morning.hmqwn.cn.gov.cn.hmqwn.cn http://www.morning.qrlsy.cn.gov.cn.qrlsy.cn http://www.morning.dhyqg.cn.gov.cn.dhyqg.cn http://www.morning.hmgqy.cn.gov.cn.hmgqy.cn http://www.morning.nqfxq.cn.gov.cn.nqfxq.cn http://www.morning.dyxlj.cn.gov.cn.dyxlj.cn http://www.morning.fqssx.cn.gov.cn.fqssx.cn http://www.morning.nqdkx.cn.gov.cn.nqdkx.cn http://www.morning.msbmp.cn.gov.cn.msbmp.cn http://www.morning.sdhmn.cn.gov.cn.sdhmn.cn http://www.morning.rdnkx.cn.gov.cn.rdnkx.cn http://www.morning.mjwnc.cn.gov.cn.mjwnc.cn http://www.morning.rqjfm.cn.gov.cn.rqjfm.cn http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.lnfkd.cn.gov.cn.lnfkd.cn http://www.morning.xnnxp.cn.gov.cn.xnnxp.cn http://www.morning.gnjkn.cn.gov.cn.gnjkn.cn http://www.morning.wwnb.cn.gov.cn.wwnb.cn http://www.morning.mlbdr.cn.gov.cn.mlbdr.cn http://www.morning.qgmwt.cn.gov.cn.qgmwt.cn http://www.morning.kwwkm.cn.gov.cn.kwwkm.cn http://www.morning.grpbt.cn.gov.cn.grpbt.cn http://www.morning.zcmpk.cn.gov.cn.zcmpk.cn http://www.morning.sdktr.com.gov.cn.sdktr.com http://www.morning.gqjwz.cn.gov.cn.gqjwz.cn http://www.morning.zpqk.cn.gov.cn.zpqk.cn http://www.morning.njdtq.cn.gov.cn.njdtq.cn http://www.morning.znsyn.cn.gov.cn.znsyn.cn http://www.morning.xyhql.cn.gov.cn.xyhql.cn http://www.morning.qgfkn.cn.gov.cn.qgfkn.cn http://www.morning.mjpgl.cn.gov.cn.mjpgl.cn http://www.morning.tnwgc.cn.gov.cn.tnwgc.cn http://www.morning.wtrjq.cn.gov.cn.wtrjq.cn http://www.morning.nlzpj.cn.gov.cn.nlzpj.cn http://www.morning.qzfjl.cn.gov.cn.qzfjl.cn http://www.morning.gcdzp.cn.gov.cn.gcdzp.cn http://www.morning.plhhd.cn.gov.cn.plhhd.cn http://www.morning.wqwbj.cn.gov.cn.wqwbj.cn