Problem Description
给定N(N≤10^5)个整数,要求用快速排序对数据进行升序排列,注意不得使用STL。
Input
连续输入多组数据,每组输入数据第一行给出正整数N(≤10^5),随后给出N个整数,数字间以空格分隔。
Output
输出排序后的结果,数字间以一个空格间隔,行末不得有多余空格。
Example Input
8 49 38 65 97 76 13 27 49
Example Output
13 27 38 49 49 65 76 97
#include<stdio.h>
void f(int a[],int left,int right)
{
int x=a[left],i=left,j=right;
if(i>=j) return;
while(i<j)
{
while(i<j&&a[j]>=x)
j--;
a[i]=a[j];
while(i<j&&a[i]<=x)
i++;
a[j]=a[i];
}
a[i]=x;
f(a,left,i-1);
f(a,i+1,right);
}
int main()
{
int n,a[100010],i;
while(~scanf("%d",&n))
{
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
f(a,0,n-1);
for (i=0;i<n;i++)
{
if(i==n-1)
printf("%d\n",a[i]);
else
printf("%d ",a[i]);
}
}
return 0;
}
本文介绍了一种不使用STL的快速排序算法实现方法,并通过示例详细解释了其工作原理及具体步骤。提供了完整的C语言代码示例,适用于对大量整数数据进行升序排序。
771

被折叠的 条评论
为什么被折叠?



