#include <iostream>
using namespace std;
int getdiv1(int* array, int left, int right)//horn
{
if (left==right)
{
return right;
}
int div=right;
int end=right-1;
int begin=left;
while (begin<end)
{
while (array[begin]<array[div] && begin<end)
{
begin++;
}
while (array[end]>array[div] && begin<end)
{
end--;
}
swap(array[begin],array[end]);
}
if (begin==end)
{
if (array[begin]<array[div])//找了一圈没找到,也就是div正好是此序列最大值
{
return div;
}
swap(array[begin],array[div]);
return begin;
}
return right;
}
int getdiv2(int* array, int left, int right)//挖坑
{
if (left==right)
{
return right;
}
int key=array[right];
int begin=left;
int end=right;
while (begin<end)
{
while (array[begin]<key && begin<end)
{
begin++;
}if (begin!=end)
{
array[end]=array[begin];
end--;
}
while (array[end]>key && begin<end)
{
end--;
}
if (begin!=end)
{
array[begin]=array[end];
begin++;
}
}
if (begin==end)
{
array[begin]=key;
return begin;
}
return right;
}
void QuickSort(int *array, int left,int right)//快排
{
if (left<right)
{
//int div=getdiv1(array,left,right);
int div=getdiv2(array,left,right);
QuickSort(array,left,div-1);
QuickSort(array,div+1,right);
}
}
C++实现快排
最新推荐文章于 2024-06-22 23:42:38 发布