博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
分治法之快速排序(以及快速排序的优化)(2021/1/24)
阅读量:676 次
发布时间:2019-03-17

本文共 1275 字,大约阅读时间需要 4 分钟。

问题引入

快速排序以及其优化

代码实现

#include
#include
using namespace std;struct Data{
int flag;};//快速排序划分函数,返回基准元素最后的下标 int Partition(struct Data*list,int low,int high){
struct Data temp; int i=low,j=high,p=low; while(i
i&&list[j].flag>list[p].flag){
j--; } //进行元素交换 if(j>i){
temp=list[j]; list[j]=list[i]; list[i]=temp; p=j; i++; } //从左向右 while(j>i&&list[i].flag<=list[p].flag){
i++; } //进行元素交换 if(j>i){
temp=list[i]; list[i]=list[j]; list[j]=temp; p=i; j--; } } return p; }//快速排序递归函数void QuickSort(struct Data*list,int low,int high){
if(low
base.flag){
j--; } //向右扫描 while(i
<=base.flag){
i++; } //交换 if(i
base.flag){ //基准元素小于i,j的位置flag,则要将基准元素放在i-1位置 temp=list[i-1]; list[i-1]=list[low]; list[low]=temp; return i-1; }else{ //交换i与low temp=list[i]; list[i]=list[low]; list[low]=temp; return i; } }//快速排序Provoid QuickSort_Pro(struct Data*list,int low,int high){ if(low

程序输出

Test Data: 0 1 6 3 12 5 18 7 24 9Use QuickSort: 0 1 3 5 6 7 9 12 18 24Test Data: 0 1 6 3 12 5 18 7 24 9Use QuickSort: 0 1 3 5 6 7 9 12 18 24--------------------------------Process exited after 0.04895 seconds with return value 0请按任意键继续. . .

转载地址:http://qbshz.baihongyu.com/

你可能感兴趣的文章