#include <stdio.h>
#include <stdlib.h>
void Qsort(int number[],int startPos, int endPos)
{
int i,j,temp;
temp=number[startPos];
i=startPos; j=endPos;
while (i<j)
{
while (number[j]>=temp && i<j)--j;
number[i]=number[j];
while (number[i]<=temp && i<j)++i;
number[j]=number[i];
}
number[i]=temp;
if (i-1>startPos) Qsort(number,startPos,i-1);
if (endPos>i+1) Qsort(number,i+1,endPos);
}
int number[1000000];
int main()
{
int n = 1000000, i;
int ri = 100, itest;
for (itest = 1; itest <= 10; ++ri, ++itest)
{
srand(ri);
for (i=0; i<n; i++)
number[i] = rand();
Qsort(number, 0, n-1);
for (i=1; i<n; i++)
{
if (number[i-1] > number [i])
{
puts("algorithm wrong");
return 0;
}
}
printf("Test %d pass\n", itest);
}
system("pause");
return 0;
}
测试似乎没有错
#include <stdlib.h>
void Qsort(int number[],int startPos, int endPos)
{
int i,j,temp;
temp=number[startPos];
i=startPos; j=endPos;
while (i<j)
{
while (number[j]>=temp && i<j)--j;
number[i]=number[j];
while (number[i]<=temp && i<j)++i;
number[j]=number[i];
}
number[i]=temp;
if (i-1>startPos) Qsort(number,startPos,i-1);
if (endPos>i+1) Qsort(number,i+1,endPos);
}
int number[1000000];
int main()
{
int n = 1000000, i;
int ri = 100, itest;
for (itest = 1; itest <= 10; ++ri, ++itest)
{
srand(ri);
for (i=0; i<n; i++)
number[i] = rand();
Qsort(number, 0, n-1);
for (i=1; i<n; i++)
{
if (number[i-1] > number [i])
{
puts("algorithm wrong");
return 0;
}
}
printf("Test %d pass\n", itest);
}
system("pause");
return 0;
}
测试似乎没有错
樱之雪,晓之车