本文共 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/