You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#define N 10
|
|
|
|
|
void display(int array[], int maxlen)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for(i = 0; i < maxlen; i++)
|
|
|
|
|
{
|
|
|
|
|
printf("%-3d", array[i]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
void QuickSort(int *arr, int low, int high)
|
|
|
|
|
{
|
|
|
|
|
if (low < high)
|
|
|
|
|
{
|
|
|
|
|
int i = low;
|
|
|
|
|
int j = high;
|
|
|
|
|
int k = arr[low];
|
|
|
|
|
while (i < j)
|
|
|
|
|
{
|
|
|
|
|
while(i < j && arr[j] >= k)
|
|
|
|
|
{
|
|
|
|
|
j--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(i < j)
|
|
|
|
|
{
|
|
|
|
|
arr[i++] = arr[j];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while(i < j && arr[i] < k)
|
|
|
|
|
{
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(i < j)
|
|
|
|
|
{
|
|
|
|
|
arr[j--] = arr[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arr[i] = k;
|
|
|
|
|
QuickSort(arr, low, i - 1);
|
|
|
|
|
QuickSort(arr, i + 1, high);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
int array[N] ;
|
|
|
|
|
int n;
|
|
|
|
|
scanf("%d",&n);
|
|
|
|
|
for(int i=0;i<n;i++)
|
|
|
|
|
scanf("%d",&array[i]);
|
|
|
|
|
int maxlen =n;
|
|
|
|
|
QuickSort(array, 0, maxlen-1);
|
|
|
|
|
|
|
|
|
|
printf("排序后的数组\n");
|
|
|
|
|
display(array, maxlen);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}/*快速排序:时间复杂度 :O(nlog2n)
|
|
|
|
|
此类排序方法不稳定
|
|
|
|
|
是最常用的排序方法
|
|
|
|
|
输入五个数字运行时间结果:Process exited after 6.263 seconds with return value 0*/
|
|
|
|
|
|
|
|
|
|
|