0

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 ();
}
6
Contributors
12
Replies
14
Views
7 Years
Discussion Span
Last Post by sourabhtripathi
Featured Replies
  • 1
    Narue 5,707   7 Years Ago

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

  • 2
    Narue 5,707   7 Years Ago

    [B]>can you please explain the whole code ? i'm just stuck !![/B] 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 [I]specific[/I] questions if you still need help. Read More

0

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);

Edited by gerard4143: n/a

1

>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?

Edited by Narue: n/a

0

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.

0

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;
       		 }
    	 }
   }
0

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])

2

>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.

1

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.

0

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 ?

1

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! ;)

0

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 ?

-1
#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;
}
}
}



}

Edited by Nick Evan: added code-tags (again)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.