//交换
void Swap(int arr[], int low, int high)
{
int temp;
temp = arr[low];
arr[low] = arr[high];
arr[high] = temp;
}
int Partition(int arr[], int low, int high)
{
int ch,base;
srand((unsigned)time(0));
ch=rand()%(high-low+1)+low;
base=arr[ch];
//int base = arr[low];
while(low < high)
{
while(low < high && arr[high] >= base)
{
high --;
}
Swap(arr, low, high);
while(low < high && arr[low] <= base)
{
low ++;
}
Swap(arr, low, high);
}
return low;
}
//分治
void QuickSort(int arr[], int low, int high)
{
if(low < high)
{
int base = Partition(arr, low, high);
QuickSort(arr, low, base - 1);
QuickSort(arr, base + 1, high);
}
}
int main()
{
int arr[n];
int i ,x, j;
x=n;
printf("请输入%d个整数:\n",x);
for(i = 0; i < n; i ++)
{
scanf("%d",&arr[i]);
}
printf("\n");
QuickSort(arr, 0, n-1);
for(j = 0; j < n; j ++)
{
printf("%4d",arr[j]);
}
return 0;
}