【问题描述】对待排序序列使用快速排序算法进行排序,计算第一次划分之后分界元素在序列中的位置和最终排序结果(划分和分界元素的概念参照课本)(在序列中的位置跟书上一致,从1而不是从0开始)
【输入形式】序列元素个数,序列
【输出形式】第一次划分之后分界元素的位置,最终排序结果
【样例输入】5 0 2 1 -1 -2
【样例输出】3 -2 -1 0 1 2
【样例说明】序列中的元素均为整数
【评分标准】
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int length;
int *elem;
}SqList;
int Part(SqList *s,int low ,int high)
{
s->elem[0]=s->elem[low];
int p=s->elem[low];
while(low<high)
{
while(low<high&&s->elem[high]>=p)
high--;
s->elem[low]=s->elem[high];
while(low<high&&s->elem[low]<=p)
low++;
s->elem[high]=s->elem[low];
}
s->elem[low]=s->elem[0];
return low;
}
void QuickSort(SqList s,int low,int high)
{
if(low<high)
{
int p=Part(&s,low,high);
QuickSort(s,low,p-1);
QuickSort(s,p+1,high);
}
}
int main()
{
SqList s;
s.elem=(int *)malloc(100*sizeof(int));
scanf("%d",&s.length);
int i;
for(i=1;i<=s.length;i++)
scanf("%d",&s.elem[i]);
int mid=Part(&s,1,s.length);
QuickSort(s,1,s.length);
printf("%d ",mid);
for(i=1;i<=s.length;i++)
printf("%d ",s.elem[i]);
}