代码:
#include<iostream>
using namespace std;
int Partion(int *arr,int low,int high)//划分过程
{
if(arr == NULL || low>=high || low<0 || high<0)
return -1;
int tmp = arr[low];
while(low < high)
{
while((arr[high]>=tmp) && (low<high))
{
high --;
}
if(low == high)
break;
else
arr[low] = arr[high];
while((arr[low]<=tmp) && (low<high))
{
low ++;
}
if(low == high)
break;
else
arr[high] = arr[low];
}
arr[low] = tmp;
return low;
}
/递归
void Quick(int *arr,int low,int high)
{
int par = Partion(arr,low,high);
if(par != -1)
{
if(low<par-1)
{
Quick(arr,low,par-1);
}
if(par<high)
{
Quick(arr,par+1,high);
}
}
}
void QuickSort(int *arr,int len)
{
if(arr == NULL || len <= 0)
return;
Quick(arr,0,len-1);
}
int main()
{
int arr[] = {71,1,2,0,8,9,9,10,3,89};
int len = sizeof(arr)/sizeof(arr[0]);
QuickSort(arr,len);
for(int i=0;i<len;++i)
{
cout<<arr[i]<<" ";
}
return 0;
}
快速排序
最新推荐文章于 2021-11-19 16:57:43 发布