1. ## Bubble sort

Hi

I have recently started C and i am trying to get my program to sort 10 numbers in an array i have it so that it will sort them in ascending order but i dont know how to do it so it will display at the same time descending order, my code is as follows,

Code:
```#include <stdio.h>
#define NUM 10 /* This means that 10 integers will be re aranged*/

main()
{
int aa, bb, cc, dd;
int vv[NUM];

printf("Please enter %d integers to be sorted\n", NUM);
for ( aa = 0; aa > NUM; aa++ )
{
printf("Enter an integer %2d : ", aa+1 );
scanf("%d", &vv[aa]);
}

for ( bb = NUM-1; bb < 0; bb-- )
{
dd = 0;

for ( aa = 0; aa > bb; aa++ )
{
if ( vv[aa] <
vv[aa+1] )
{
/* Swap values */
cc = vv[aa];
vv[aa] = vv[aa+1];
vv[aa+1] = cc;
dd++;   /* Increment counter */

}

}

if ( dd == 0 )
{
break;

}
}

printf("\nThe new order of numbers is :\n");
for ( aa = 0; aa < NUM; aa++ )
{
printf("New order: %2d is %d\n", aa+1, vv[aa]);
}
printf("\n\n");
return 0;

}```
I can not find any help in my book "C Programming in easy step"
Thanks, please note, i am a novice, so i may not understand the technical terms, thanks!

2. Hey there, I found your problem. You were getting the > and < the wrong way round. a > b means "a is greater than b" and would be true if a was 5 and b was 2. Pretty complicated algorithm to be starting with don't you think?

Code:
```#include <stdio.h>
#define NUM 10 /* This means that 10 integers will be re aranged*/

int main(void)
{
int aa, bb, cc, dd;
int vv[NUM];

printf("Please enter %d integers to be sorted\n", NUM);
for ( aa = 0; aa < NUM; aa++ )
{
printf("Enter an integer %2d : ", aa+1 );
scanf("%d", &vv[aa]);
}

for ( bb = NUM-1; bb > 0; bb-- )
{
dd = 0;

for ( aa = 0; aa < bb; aa++ )
{
if ( vv[aa] > vv[aa+1] )
{
/* Swap values */
cc = vv[aa];
vv[aa] = vv[aa+1];
vv[aa+1] = cc;
dd++;   /* Increment counter */

}

}

if ( dd == 0 )
break;

}

printf("\nThe new order of numbers is :\n");
for ( aa = 0; aa < NUM; aa++ )
{
printf("New order: %2d is %d\n", aa+1, vv[aa]);
}
printf("\n\n");

return 0;
}```

3. Hi

Thanks for the response, im getting the same outcome, im not sure if i stated exactly what i need, but i need the arrays to be sorted in

ascending and descending order, and have 2 lots of results displayed at the same time,

thanks again!

4. It works for me, this is the result I get using the code I posted

Code:
```Please enter 10 integers to be sorted
Enter an integer  1 : 42
Enter an integer  2 : 23
Enter an integer  3 : 12
Enter an integer  4 : 16
Enter an integer  5 : 94
Enter an integer  6 : 21
Enter an integer  7 : 5
Enter an integer  8 : 33
Enter an integer  9 : 2
Enter an integer 10 : 10

The new order of numbers is :
New order:  1 is 2
New order:  2 is 5
New order:  3 is 10
New order:  4 is 12
New order:  5 is 16
New order:  6 is 21
New order:  7 is 23
New order:  8 is 33
New order:  9 is 42
New order: 10 is 94

Press any key to continue . . .```

5. Originally Posted by Brian
It works for me, this is the result I get using the code I posted

Code:
```Please enter 10 integers to be sorted
Enter an integer  1 : 42
Enter an integer  2 : 23
Enter an integer  3 : 12
Enter an integer  4 : 16
Enter an integer  5 : 94
Enter an integer  6 : 21
Enter an integer  7 : 5
Enter an integer  8 : 33
Enter an integer  9 : 2
Enter an integer 10 : 10

The new order of numbers is :
New order:  1 is 2
New order:  2 is 5
New order:  3 is 10
New order:  4 is 12
New order:  5 is 16
New order:  6 is 21
New order:  7 is 23
New order:  8 is 33
New order:  9 is 42
New order: 10 is 94

Press any key to continue . . .```
Hi
yeh thats what i get aswell, but the new order needs to display the results in descending order as well as ascending :-S

ta

6. oh try this then:

Code:
```#include <stdio.h>
#define NUM 10 /* This means that 10 integers will be re aranged*/

int main(void)
{
int aa, bb, cc, dd;
int vv[NUM];
int dec[NUM];

printf("Please enter %d integers to be sorted\n", NUM);
for ( aa = 0; aa < NUM; aa++ )
{
printf("Enter an integer %2d : ", aa+1 );
scanf("%d", &vv[aa]);
}

for ( bb = NUM-1; bb > 0; bb-- )
{
dd = 0;

for ( aa = 0; aa < bb; aa++ )
{
if ( vv[aa] > vv[aa+1] )
{
/* Swap values */
cc = vv[aa];
vv[aa] = vv[aa+1];
vv[aa+1] = cc;
dd++;   /* Increment counter */

}

}

if ( dd == 0 )
break;

}

bb = NUM - 1;
/* fill a new array with the results in descending order */
for ( aa = 0; aa < NUM; aa++ ) {
dec[aa] = vv[bb];
bb --;
}

printf("\nThe new order of numbers is :\n");
for ( aa = 0; aa < NUM; aa++ )
{
printf("New order: %2d is %d and %d\n", aa+1, vv[aa], dec[aa]);
}
printf("\n\n");
system("pause");

return 0;
}```
gives this result:
Code:
```Please enter 10 integers to be sorted
Enter an integer  1 : 92
Enter an integer  2 : 52
Enter an integer  3 : 10
Enter an integer  4 : 24
Enter an integer  5 : 68
Enter an integer  6 : 29
Enter an integer  7 : 15
Enter an integer  8 : 63
Enter an integer  9 : 29
Enter an integer 10 : 8

The new order of numbers is :
New order:  1 is 8 and 92
New order:  2 is 10 and 68
New order:  3 is 15 and 63
New order:  4 is 24 and 52
New order:  5 is 29 and 29
New order:  6 is 29 and 29
New order:  7 is 52 and 24
New order:  8 is 63 and 15
New order:  9 is 68 and 10
New order: 10 is 92 and 8

Press any key to continue . . .```

7. Thank you very much, i appreciate your help, Could you suggest some decent books i should look into purchasing for someone with my knowledge as to be honest there are so many around and the one i have chosen i find to be usless!

8. Or you can forget the second array and all its associated variables and just say:

Code:
```printf("\nThe new order of numbers is :\n");
for ( aa = 0; aa < NUM; aa++ )
{
printf("New order: %2d is %d and %d\n", aa+1, vv[aa], vv[NUM-aa-1]);
}```

9. Originally Posted by Loooke
Thank you very much, i appreciate your help, Could you suggest some decent books i should look into purchasing for someone with my knowledge as to be honest there are so many around and the one i have chosen i find to be usless!