Rember that it 's a wrong way to do this like:
Increase range:
for(int i=0;i<100;i++)
{ int x=a[i] ;
for(int j=i+1;j<100;j++)
{ int y=a[j];
if(y<x)
{ int t=a[j];a[j]=a[i];a[i]=t;}
}
}
Why that's wrong??
The reason is a[i] in the loop has been changed, but the x isn't changed. so It's wrong !
you can do it correctly like:
for(int i=0;i<100;i++)
{
for(int j=i+1;j<100;j++)
{ int x=a[i] ;
int y=a[j];
if(y<x)
{ int t=a[j];a[j]=a[i];a[i]=t;}
}
}
or
for(int i=0;i<100;i++)
{ int x=a[i] ;
for(int j=i+1;j<100;j++)
{
int y=a[j];
if(y<x)
{ x=y; int t=a[j];a[j]=a[i];a[i]=t;}
}
}
No comments:
Post a Comment