Hi there,

I was trying to code a simple program that bubble sorts an array into a list of numbers arranged in a descending order:

Here is the code I used:

``````#define size1 5
#define size2 5

#include <stdio.h>
int main()
{
int a[size1]={8,4,5,2,3};//array 1
int i=0;
int n=0;
int temp=0;

//sort array A into a descending sequence
for(i=0;i<size1;i++)
{
for(n=i+1;n<size1;n++)
{
if (a[i]<a[n])
{temp = a[i];a[i]=a[n];a[n]=temp;}
}

}

for (i=0;i<size1;i++)
{
printf("%d\n",a[i]);
}

return(0);
}``````

Now the block of code written above works just fine, however if I replace

``for(n=i+1;n<size1;n++)``

with

``for(n=1;n<size1;n++)``

..the program randomly generates a sequence of numbers along with a garbage value. What's going on here? As far as I can see both codes are equivalent.

Any input will be appreciated.

what is the reason that made u feel that the statements

``for(n=i+1;n<size1;n++)``

and

``for(n=1;n<size1;n++)``

are equivalent.

How does 1 and i+1 becomes same.

That is the logic and u are changing the whole logic for sorting.

foa i believe this is not a bubble sort .

and

for(n=1;n<size1;n++)

is worng because

in the process all ready sorted ( in descending order ) elements again go up.

when i =0 n goes from 1-4
n = 1 to 4 all are less than 8 no replacements

when i =1 n goes from 1-4
n =1 no replacement
n=2, i=1; 4<5 : replacement, so list will be 8 5 4 2 3
n= 3,4 no replacement

when i =2 n goes from 1-4
i =2 , n =1 ;4 < 5 : replacement, so list will be 8 4 5 2 3 (wrong)
n =2,3,4 no replacement

when i =3 n goes from 1-4
i = 3, n =1 ; 2 < 4 : replacement , so list will be 8 2 5 4 3
i =3, n = 2 ; 4 < 5 : replacement , so list will be 8 2 4 5 3
n = 3 , 4 no change

when i =4 n goes from 1-4
i = 4, n =1 ; no change
i = 4 ,n = 2; 3<4 ; replacement , so list will be 8 2 3 5 4
i =4, n =3 4 < 5 replacement , so list will be 8 2 3 4 5
i = 4 n =4 no change
so the final out put
8 2 3 4 5

the program randomly generates a sequence of numbers along with a garbage value.

i am not getting any garbages

the correct statement is

``````for(i=0;i<size1-1;i++)
{
for(n=0;n<size1-1;n++)
{
if (a[n]<a[n+1])
{
temp = a[n];
a[n]=a[n+1];
a[n+1]=temp;
}
}
}``````

that is simple one
we can make the sorting faster than above.

Well when the initial loop starts off, i is set to zero right? And i pretty much specifies which element in the array must be compared with the ones succeeding it.

So lets say the element to be compared has an address of 0; the first data element to succeed it will have an address of 1. Which is the same as i+1... but wait, if we go by my logic; on the second iteration- the element to be compared will have an address of 1 and the nested loop will try comparing it with its own value.

Apologize for any grammatical mistakes, thanks for helping me figure it out.

That sounds good.

In the 1st iteration it will be 1, in the second n=i+1 = 2 because i==1 now aand so on. But in the later case n will always be 1 because u did n=1.