#include<stdio.h>
#include<stdlib.h>
typedef int KeyType;
typedef struct {
KeyType *elem; /*elem[0]一般作哨兵或缓冲区*/
int Length;
}SqList;
void CreatSqList(SqList *L);/*待排序列建立,由裁判实现,细节不表*/
int Partition ( SqList L,int low, int high );
void Qsort ( SqList L,int low, int high );
int main()
{
SqList L;
int i;
CreatSqList(&L);
Qsort(L,1,L.Length);
for(i=1;i<=L.Length;i++)
{
printf("%d ",L.elem[i]);
}
return 0;
}
void Qsort ( SqList L,int low, int high )
{ int pivotloc;
if(low<high)
{
pivotloc = Partition(L, low, high ) ;
Qsort (L, low, pivotloc-1) ;
Qsort (L, pivotloc+1, high );
}
}
int Partition ( SqList L,int low, int high )
{
int mid = L.elem[low];
while(low < high){
while(low<high&&L.elem[high]>=mid) high --;
L.elem[low] = L.elem[high];
while(low<high&&L.elem[low]<=mid) low ++;
L.elem[high] = L.elem[low];
}
L.elem[low] = mid;
return low;
}
/*你的代码将被嵌在这里 */
6-2 快速排序
最新推荐文章于 2023-02-14 12:44:18 发布