void Swap(int *a,int *b)
{
int tem = *a;
*a = *b;
*b = tem;
}
int Partition(int *a, int s, int e)
{
int key = a[e];
int i = s;
int j = s;
for(;j < e; j++)
{
if(a[j] < key)
Swap(&a[j], &a[i++]);
}
Swap(&a[i],&a[e]);
return i;
}
int Partition2(int *a, int s, int e)
{
int key = a[s];
int i = s;
int j = e;
while( i < j )
{
while( i < j && a[j] >= key )
j--;
if(i < j)
a[i++] = a[j];
while( i < j && a[i] < key )
i++;
if(i < j)
a[j--] = a[i];
}
a[i] = key;
return i;
}
void QuickSort(int *a, int s, int e)
{
if( s < e )
{
int m = Partition2(a,s,e);
QuickSort(a,s,m-1);
QuickSort(a,m+1,e);
}
}
快速排序
最新推荐文章于 2024-07-17 10:35:12 发布