Friday, 9 August 2013

Which sorting technique is this code?

Which sorting technique is this code?

Someone asked me to sort an array, which I did as follows. Now we're
fighting over which sorting technique this is. He's classifying it as
bubble after explaining me the different sorting techniques he knows
about, but I think it's not! But it does sort!
C code:
void sort(void){
int a[9]={4,2,1,3,5,7,5,6,8};
int i,j,temp;
for(i=0;i<9;i++)
{
for(j=0;j<i;j++)
{
if(a[j] > a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for(i=0;i<9;i++)
{
printf("\n%d",a[i]);
}
}
This is bubble, according to me which he agrees to, but also classifies
the former as the same. I mean there's got to be a name for that!
for(i=0;i<9;i++)
{
for(j=0;j<8;j++)
{
if(a[j] > a[j+1])
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}

No comments:

Post a Comment