sorting

Please clear my doubt. I used the following logic to sort the numbers in ascending order... The logic seems to be right(correct me if i'm wrong).
1
2
3
4
5
6
7
8
9
10
11
12
13
for(j=0;j<10;j++)
{
    min=a[0];
    for(i=0;i<10;i++)
    {
        if(a[i]<min&&a[i]>a[x])
        {
	 min=a[i];
	 x=i;
        }
    }
   cout<<"\n"<<min;
}


But i don't get the expected output. I've initialized x to 0 in the beginning.
since you initialize x to 0, this means
 
if(a[0]<min&&a[0]>a[0])

no element of 'a' is changed so you don't sort anything.
Can you please say why the elements of 'a' is not changed?
that 'a[...]' is not modified should be quite obvious. If you don't want to then you logic is wrong:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int x = INT_MIN; // note
for(j=0;j<10;j++)
{
    min=INT_MAX; // note
    for(i=0;i<10;i++)
    {
        if(a[i]<min&&a[i]>x) // note x not a[x]
        {
	 min=a[i];
        }
    }
    x = min; // note
   cout<<"\n"<<min;
}
That should work (not tested though)
Last edited on

Can you please say why the elements of 'a' is not changed?


1
2
3
4
5
6
7
8
9
10
11
12
13
for(j=0;j<10;j++)  // Assigning values to j
{
    min=a[0]; // Assigning value to min
    for(i=0;i<10;i++) // Assigning values to i
    {
        if(a[i]<min&&a[i]>a[x])
        {
	 min=a[i]; // Assigning value to min
	 x=i; // Assigning value to x
        }
    }
   cout<<"\n"<<min;
}


I've marked in your code everywhere that you assign a value. You don't assign a value to 'a' anywhere, so how can you expect it to be changed?
Thank you guys... :)
Topic archived. No new replies allowed.