Sicily1154(快排)

#include <iostream>
#include <cstdio>
using namespace std;

int partition(int *A,int l,int r)
{
	int x=A[r];
	int i,j;
	int temp;
	i=l-1;
	for (j=l;j<=r-1;j++)
		if (A[j]<=x)
		{
			i=i+1;
			temp=A[j];
			A[j]=A[i];
			A[i]=temp;
		}
	temp=A[i+1];
	A[i+1]=A[r];
	A[r]=temp;
	return i+1;
}

void quick_sort(int *A,int l,int r)
{
	int mid;
  if (l<r)
  {
  	mid=partition(A,l,r);
  	quick_sort(A,l,mid-1);
  	quick_sort(A,mid+1,r);
  }
}

int main()
{
	int T;
	scanf("%d",&T);
	while (T--)
	{
		int n;
		scanf("%d",&n);
		int i;
		int A[1005];
		for (i=0;i<=n-1;i++)
			scanf("%d",&A[i]);
		quick_sort(A,0,n-1);
		for (i=0;i<=n-1;i++)
			printf("%d\n",A[i]);
	}
	return 0;
}


你可能感兴趣的:(Sicily1154(快排))