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(in t 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(in t sorted[])
{
int a=0,b=0;
while(a <= SIZE-1)
{
b= a + 1;
if (sorted[a] > sorted[b])
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[i] = temp;
else
continue;
i++;
}//end while
printf("\norigi nal-->\n");
for(i=0;i<=SIZE-1;i++)
printf(" %d ",bu[i]);
printf("\n");
for (i=0;i<=SIZE-2;i++)
for (j=i+1;j<=SIZE-1;j++)
{
if(bu[i]>bu[j])
{
temp=bu[i];
bu[i]=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[i]);
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(in t 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(in t sorted[])
{
int a=0,b=0;
while(a <= SIZE-1)
{
b= a + 1;
if (sorted[a] > sorted[b])
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[i] = temp;
else
continue;
i++;
}//end while
printf("\norigi nal-->\n");
for(i=0;i<=SIZE-1;i++)
printf(" %d ",bu[i]);
printf("\n");
for (i=0;i<=SIZE-2;i++)
for (j=i+1;j<=SIZE-1;j++)
{
if(bu[i]>bu[j])
{
temp=bu[i];
bu[i]=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[i]);
printf("\n");
return 0;
}/* end main*/
Comment