//快速排序是对冒泡排序的一种改进,是基于分治的。(不稳定算法)
#include<iostream>
using namespace std;
//一趟快速排序的算法为:
int Partition(int a[],int low,int high)
{
int pivot = a[low];
while(low<high)
{
while(low<high && a[high]>=pivot) --high;
a[low] = a[high];//将比枢轴值小的元素移到左边
while(low<high && a[low]<=pivot) ++low;
a[high] = a[low];
}
a[low] = pivot;
return low;
}
//快排的递归算法
void QuickSort(int a[],int low,int high)
{
if(low<high)//边界条件,即递归跳出的条件
{
int pivotos = Partition(a,low,high);//划分
QuickSort(a,low,pivotos-1);
QuickSort(a,pivotos+1,high);
}
}
void main()
{
int s[6]={9,3,4,10,2,11};
QuickSort(s,0,5);
for(int i=0;i<6;i++)
cout<<s[i]<<' ';
cout<<endl;
}
快速排序
最新推荐文章于 2024-02-29 12:05:41 发布