sorting in array,
i'm stuck understand that why we've used a 'j' loop here ?
can anybody explain ? and why we are checking this condition ? if(num[i]>num[j])

int main(void)
{
   int num[5],temp;
   int i,j;
   for(i=0;i<5;i++)
	{
	printf("Enter Value for %d",i);
	scanf("%d",&num[i]);
   	}
   for(i=0;i<5;i++)
   {
       for (j=0;j<5;j++)
       	 {
      	 if(num[i]>num[j])
       		 {
	         temp=num[i];
        	 num[i]=num[j];
	         num[j]=temp;
       		 }
    	 }
   }
   for(i=0;i<5;i++)
  	{
	printf("%d ",num[i]);
	}
getche ();
}

Recommended Answers

All 12 Replies

The second loop allows us to check each element against all the other elements.

Just a note:

printf("Enter Value for %d",i);

The printf should be

printf("Enter Value for element [%d]->", i);

>and why we are checking this condition ? if(num[i]>num[j]) Why would you want to swap items that are already in the correct order?

In your sorting code, when j is less than, or equal to i, the comparisons are wasted - they're already sorted at that point.

So, in the inner loop:

//same for loop as before, right here
for(j = i + i, j < 5; j++) {

  // same code as before in here
}

will do the same job, quicker. ;)

This, btw, is called Selection sort.

i didn't understand the code, please explain

This is the sorting portion of your code, from above:

for(i=0;i<5;i++)
   {
       for ([B]j=0;[/B]j<5;j++)
       	 {
      	 if(num[i]>num[j])
       		 {
	         temp=num[i];
        	 num[i]=num[j];
	         num[j]=temp;
       		 }
    	 }
   }
/* 
and this is the same code, optimized. It is called Selection sort (goto Wikipedia for a whole page on it).
*/
   for(i=0;i<5;i++)
   {
       for ([B]j = i + 1;[/B]j<5;j++)
       	 {
      	 if(num[i]>num[j])
       		 {
	         temp=num[i];
        	 num[i]=num[j];
	         num[j]=temp;
       		 }
    	 }
   }

hey, can you please explain the whole code ? i'm just stuck !!
on line 8 i entered value for num for example i entered 2 , now with what value of 'j' will it be compare in this line ? if(num[i]>num[j])

>can you please explain the whole code ? i'm just stuck !!
Nobody is going to explain the world to you just because you're clueless. Turn on your brain and figure it out yourself, or come up with specific questions if you still need help.

With nested loops, the comparisons become:
a to a[j]
==========
a[0] to a[1]
a[0] to a[2]
a[0] to a[3]
a[0] to a[4] <<j loop exits, after this comparison
a[1] to a[2]
a[1] to a[3]
a[1] to a[4] << j loop exits again, after this comparison
a[2] to a[3]
a[2] to a[4] << j loop exits again, after this comparison
a[3] to a[4] << last break out from the j loop, because j is no longer < 5
Now, the i loop breaks out because i is no longer < 5
============================================
End of Selection sort of 5 values

I've noticed that you're having trouble with these basic concepts of loops, etc.

Study up, because most posters, will not have the time or the patience, to write up answers like this. That includes me, as well.

ok ! suppose i entered values for num : 2, 5 , 7, 9, 3
the program first check every value with the value inside the loop j ? which means it will check with the next value of num ?
for example,
here first value for num is 2 so the first value of num[j] would be 5 ?
am i right ?

ok ! suppose i entered values for num : 2, 5 , 7, 9, 3
the program first check every value with the value inside the loop j ? which means it will check with the next value of num ?
for example,
here first value for num is 2 so the first value of num[j] would be 5 ?
am i right ?

Yes!

No sense comparing 2 with 2! ;)

ok ! suppose i entered values for num : 2, 5 , 7, 9, 3
the program first check every value with the value inside the loop j ? which means it will check with the next value of num ?
for example,
here first value for num is 2 so the first value of num[j] would be 5 ?
am i right ?

Why dont you put printf statements in your code and find out for your self ?

#include<stdio.h>

int main()
{
int a[10];
sort(a);
}

void sort(int a[10])
{
for(int i=0;i<10;i++)
{ 
for(j=i+1;j<10;j++)
{
if(a[i]<a[j])
{ int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}



}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.