用快速排序写该题
代码:
#include<stdio.h>
#include<math.h>
int a[102] = {0};
void Quicksort(int left, int right)
{
int i,j,t,temp,k;
if(left > right)
return;
temp = a[left];
k = abs(a[left]);
i = left;
j = right;
while(i != j){
while(abs(a[j]) <= k && i < j)
j--;
while(abs(a[i]) >= k && i < j)
i++;
if(i < j){
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
a[left] = a[i];
a[i] = temp;
Quicksort(left, i-1);
Quicksort(i+1, right);
}
int main()
{
int i,n;
while(scanf("%d",&n) ,n != 0){
for(i = 0; i < n; i++)
scanf("%d",&a[i]);
Quicksort(0, n-1);
for(i = 0; i < n - 1; i++)
printf("%d ",a[i]);
printf("%d
",a[n-1]);
}
return 0;
}