S
Scorpio
hi,
I'm trying to run a bubble-sort. But the while() in main doesn't work.
Can somebody tell me why? thanks a lot~~~
here is the code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 20
int check_repeat(int a, int randnum, int randarray[])
{
int z=0,mark=0;
while(z<a)
{
if (randnum == randarray[z])
{
mark++;
break;
}
a++;
}
if (mark == 0)
return 1;
else
return 0;
}
void check_double(int sorted[])
{
int a=0,b=0;
while(a <= SIZE-1)
{
b= a + 1;
if (sorted[a] > sorted)
printf("**error of (sorted[%d] > sorted[%d])\n",a,b);
a++;
}
}
int main()
{
printf("start!!\n");
int bu[SIZE]={0};
int i=0,temp=0,j=0,checkpoint=0;
srand((unsigned)time(NULL));
printf("i=%d\n",i);
//HERE IS THE PROBLEM!!!
while(i<=(SIZE-1))
{
printf("in while--i=%d ",i);
temp=rand();
checkpoint = check_repeat(i,temp,bu);
if (checkpoint == 1)
bu = temp;
else
continue;
i++;
}//end while
printf("\noriginal-->\n");
for(i=0;i<=SIZE-1;i++)
printf(" %d ",bu);
printf("\n");
for (i=0;i<=SIZE-2;i++)
for (j=i+1;j<=SIZE-1;j++)
{
if(bu>bu[j])
{
temp=bu;
bu=bu[j];
bu[j]=temp;
}/* end if*/
} /* end i-for*/
check_double(bu);
printf("after-->\n");
for (i=0;i<=SIZE-1;i++)
printf(" %d ",bu);
printf("\n");
return 0;
}/* end main*/
I'm trying to run a bubble-sort. But the while() in main doesn't work.
Can somebody tell me why? thanks a lot~~~
here is the code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 20
int check_repeat(int a, int randnum, int randarray[])
{
int z=0,mark=0;
while(z<a)
{
if (randnum == randarray[z])
{
mark++;
break;
}
a++;
}
if (mark == 0)
return 1;
else
return 0;
}
void check_double(int sorted[])
{
int a=0,b=0;
while(a <= SIZE-1)
{
b= a + 1;
if (sorted[a] > sorted)
printf("**error of (sorted[%d] > sorted[%d])\n",a,b);
a++;
}
}
int main()
{
printf("start!!\n");
int bu[SIZE]={0};
int i=0,temp=0,j=0,checkpoint=0;
srand((unsigned)time(NULL));
printf("i=%d\n",i);
//HERE IS THE PROBLEM!!!
while(i<=(SIZE-1))
{
printf("in while--i=%d ",i);
temp=rand();
checkpoint = check_repeat(i,temp,bu);
if (checkpoint == 1)
bu = temp;
else
continue;
i++;
}//end while
printf("\noriginal-->\n");
for(i=0;i<=SIZE-1;i++)
printf(" %d ",bu);
printf("\n");
for (i=0;i<=SIZE-2;i++)
for (j=i+1;j<=SIZE-1;j++)
{
if(bu>bu[j])
{
temp=bu;
bu=bu[j];
bu[j]=temp;
}/* end if*/
} /* end i-for*/
check_double(bu);
printf("after-->\n");
for (i=0;i<=SIZE-1;i++)
printf(" %d ",bu);
printf("\n");
return 0;
}/* end main*/