# Thread: printing array elements in ascending order

1. plz explain me if u can.........that what r the changes that should be made in my code above in those 2 loops when the array elelments have to be printed in ascending as well as in descending order........
so that i can know the logic behind that..........
i'll be highly greatful to u........

2. Okay...one last time I'll say this....
Code:
`for(j=i-1;j<10;j++)`
You are counting too high. In the final pass of the loop, you will be comparing a[9] to a[9 +1] which doesn't exist. Array a only has elements [0..9]. That would be a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], and a[9]. As you can see...a[9 +1] (which is the same as a[10]) is NOT in that range.

Also, I have NO idea why you went from for(j=j+1; j<10; j++) to for(j=i-1; j<10; j++).

For each pass through the loop, you need to begin the bubble sort at the first element of the array which would be a[0]. So why are you trying to start the sort at a[i-1]? The initial value of j should be equivilent to the first element of the array.

edit: Aaaah, I see why you tried j=i-1, but now you're mixing pieces from somebody else's sort method, with the bubble sort that you're trying to use. I recommend you stick with the bubble sort method. Other methods are better to sort arrays, but stick with the bubble sort method as a learning tool, until you see the logic behind it.

3. i really don't know im sorry to say this.........i know u might be getting frustrating by this.......but i have to give this assignment by today itself....and i really don't have too much of time left....but i am just trying to understand what is the logic behind using the required condition in a for loop....for which i m doing repeated mistakes.........i have tried everything to make my code work......but it's just out of my reach it seems now.........coz i just don't know how it's gonna be run.........so plz just give me the answer in short......for"for" loops.
i think im grtting confused with the concepts of this incerasing and decreasing order and bubble sorting of for loops...........so could u just tell me what should be done "in this case" when i have to print in ascending order and when i'll have to print in descending order by using bubble sort. what will be the 2 "for" loops....and what does bubble sort do.....by using 2 loops...how do they actually work.....?

4. Sorry, but one thing I will NOT do is give answers for assignments.

If it's something you don't understand, I strongly urge you to discuss it with your teacher/instructor. Your instructor needs to be aware of what he/she may need to focus some attention for each student. And if I give you the answer, then you wouldn't learn anything.

5. Give up. Noone wants to read that crap you call a post. If you ever decide to post on this board again, do the following:
2) Do as they instruct.
3) Use complete sentences.
4) Punctuate properly.
5) Use whole words. Don't give us horribly abbreviated drivel.

Quzah.

6. These changes in the for loop will help.

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

PS: But, it is not the best sorting method.

7. Give up. Noone wants to read that crap you call a post. If you ever decide to post on this board again, do the following:
2) Do as they instruct.
3) Use complete sentences.
4) Punctuate properly.
5) Use whole words. Don't give us horribly abbreviated drivel.

Quzah
You seem more interested in non-technical help!!!!!

8. Originally Posted by sangi
You seem more interested in non-technical help!!!!!
Aw, birds of a feather. Did you find a friend in stupidity?

I see you're still not using code tags.

Quzah.

9. As the name of the board suggest, i would rather concentrate on technical help than giving suggestions on the style of postings.

10. Originally Posted by sangi
These changes in the for loop will help.

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

PS: But, it is not the best sorting method.
Despite not using code tags...you're wrong as well. This won't work. Try not to give the answer...especially if you don't know it.

11. yaa. right. It won't work. sorry for that.
The swap should be between a[j] and a[i].

12. Still wrong. Then entire logic structure is wrong. Here's the output you get...

Code:
```ENTER ARRAY ELELMENTS: 8 9 7 4 9 2 3 10 11 12
SORTED ARRAY IS: 8 4 2 3 7 -1073745496 9 9 10 11```
Hint: The hilited output did not come from any element in the range a[0...9]. And they still aren't sorted properly for another reason.

13. so what it would be then.........every 1 is giving their differnet opinions.............could any 1 plz tell me here that what do we mean by bubble sort ......the actual implementation of the 2 for loops that r used in it...how do they actually work....?
and how can we use the bubble sort in printing the array in ascending and descending order.............?plz plz help me out here with the concepts and logics here...........im really getting confused now!!!!!!!!!!!!!!!
any kind of help would be really appreciated..................!!!!!!!!!!!!!!!!!

14. This is the code which gives the correct results. I have tried this.
Code:
```#include<stdio.h>
int main(void)
{
int a[5],temp,i,j;
printf("ENTER ARRAY ELELMENTS:");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
fflush(stdin);
for(i=0;i<5;i++)
{
for(j=i;j<5;j++)
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
printf("SORTED ARRAY IS:");
for(i=0;i<5;i++)
printf("%d ",a[i]);
return 0;
}```

15. Seriously...how can I get more specific without giving you the answer?

`for(j=i-1;j<10;j++)`