#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#define size 30000
#define SWAP(x, y, t) ((t = x), (x = y), (y = t))
int num[size];
void quicksort(int list[], int left, int right);
int main()
{
int i;
srand(time(NULL));
for(i = 0; i < size; i++)
num[i] = rand();
for(i = 0; i < size; i++)
{
printf("%d", num[i]);
if(i % 10 == 0)
printf("\n");
else
printf(" ");
}
quicksort(num, 0, size - 1);
printf("\n-------------------------------------------------\n");
for(i = 0; i < size; i++)
{
printf("%d", num[i]);
if(i % 10 == 0)
printf("\n");
else
printf(" ");
}
return 0;
}
void quicksort(int list[], int left, int right)
{
int piovt, i, j;
int temp;
if(left < right)
{
i = left;
j = right + 1;
piovt = list[left];
do
{
do
i++;
while(list[i] < piovt);
do
j--;
while(list[j] > piovt);
if(i < j)
SWAP(list[i], list[j], temp);
}
while(i < j);
SWAP(list[left], list[j], temp);
quicksort(list, left, j - 1);
quicksort(list, j + 1, right);
}
}
快排示例
最新推荐文章于 2024-06-04 16:56:55 发布