int partition(SqList &L,int low,int high,int a)
{
int pivotkey;
ElemType e;
if(a==1){
pivotkey=L.elem[low].Num;
while(low<high)
{
while(low<high&&L.elem[high].Num>=pivotkey)
high--;
e=L.elem[low];
L.elem[low]=L.elem[high];
L.elem[high]=e;
while(low<high&&L.elem[low].Num<=pivotkey)
low++;
e=L.elem[low];
L.elem[low]=L.elem[high];
L.elem[high]=e;
}
return low;}
else if(a==2)
{
pivotkey=L.elem[low].Score;
while(low<high)
{
while(low<high&&L.elem[high].Score>=pivotkey)
high--;
e=L.elem[low];
L.elem[low]=L.elem[high];
L.elem[high]=e;
while(low<high&&L.elem[low].Score<=pivotkey)
low++;
e=L.elem[low];
L.elem[low]=L.elem[high];
L.elem[high]=e;
}
return low;
}
}
void Qsort(SqList &L,int low,int high,int a)
{
int pivot;
if(low<high)
{
pivot=partition(L,low,high,a);
Qsort(L,low,pivot-1,a);
Qsort(L,pivot+1,high,a);
}
}
void quick_sort(SqList &L,int a)
{
Qsort(L,1,L.len,a);
}
快速排序
最新推荐文章于 2022-11-28 21:30:39 发布