# Thread: Sorting in descending order.

1. ## Sorting in descending order.

I have to write a function to sort a list of numbers into ascending and descending order. I've figured out how to do it in ascending order. So i thought that to do descending order, I would just have to switch the sign from greater than, to less than but it doesn't work. After the first element it stops.

Code:
```void sort(float b[], int size, int direction)

{
float tmp;
int k,lt;

lt= size-1;

if(direction == 1){  //ascending

for (k = 0; k < size; k++){
if(b[k]>b[lt]){
tmp = b[i];
b[k] = b[lt];
b[lt] = tmp;

}
}
}
if(direction == 2){     //descending
for (k = 0; k < size; k++){
if(b[k]<b[lt]){
tmp = b[k];
b[k] = b[lt];
b[lt] = tmp;
}
}
}
}```

2. I suggest that you post your actual code. I happily setup a source file to test your function, and when I tried to compile:
Code:
```test.c: In function `sort':
test.c:12: error: `a' undeclared (first use in this function)
test.c:12: error: (Each undeclared identifier is reported only once
test.c:12: error: for each function it appears in.)
test.c:16: error: `i' undeclared (first use in this function)
test.c:23: error: `c' undeclared (first use in this function)```

3. Where are a and c declared? Or do you mean 'a' and 'c'?

And there's no way your ascending sort will work either. Both are wrong.

4. -100.000000
1.000000
2.000000
3.000000
4.000000
5.000000
7.000000
24.000000
9.000000
-4.000000
29.000000

5. november1992, this is what I tried to test with:
Code:
```#include <stdio.h>

void sort(float b[], int size, int direction)

{
float tmp;
int k,lt;

lt= size-1;

if(direction == a){  //ascending

for (k = 0; k < size; k++){
if(b[k]>b[lt]){
tmp = b[i];
b[k] = b[lt];
b[lt] = tmp;

}
}
}
if(direction == c){     //descending
for (k = 0; k < size; k++){
if(b[k]<b[lt]){
tmp = b[k];
b[k] = b[lt];
b[lt] = tmp;
}
}
}
}

void print(float numbers[], int num)
{
int i;
for (i = 0; i < num; ++i)
{
printf("%f ", numbers[i]);
}
putchar('\n');
}

int main(void)
{
float numbers[] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9};
int num = sizeof(numbers) / sizeof(numbers[0]);
print(numbers, num);
sort(numbers, num, 'd');
print(numbers, num);
sort(numbers, num, 'c');
print(numbers, num);
return 0;
}```
Notice how simple is the part that is used to test? No need for file I/O; file I/O can be added later. The whole point is to test the sort function, so I specify the test array in the code and just write to standard output.

6. Originally Posted by oogabooga
Where are a and c declared? Or do you mean 'a' and 'c'?

And there's no way your ascending sort will work either. Both are wrong.
Well, I didn't try negative numbers and it worked. It doesn't work when I put negative numbers in.

7. Oh I almost forgot there has to be a swap function above the sort.

Code:
```void swap(float *a, float *b)
{

float tmp;

tmp = *a;
*a = *b;
*b = tmp;

}```

8. Originally Posted by november1992
Well, I didn't try negative numbers and it worked. It doesn't work when I put negative numbers in.
Your code won't even compile for me, so I don't know what you are talking about.

Anyway, oogabooga is right to say that your algorithm is wrong: you are just making one pass over the array. If it worked for you, that's because your input happens to be almost sorted in one direction, hence it did not work in the other direction.

9. I'm not sure what that means, I guess I'll just ask my teacher. Thanks for trying.

10. Originally Posted by november1992
I'm not sure what that means
Go through your algorithm step by step. What does it actually do?

Or, since you claim that you "figured out how to do it in ascending order", test by sorting in ascending order an array that is completely in descending order. Does it work?

11. Originally Posted by november1992
I've figured out how to do it in ascending order.
I'm afraid I have news for you...
That code does not sort anything into asecnding order, partly because it does not compile, and partly because to actually perform a sort along the line of what you are doing would require nested loops.

Thanks for trying.
Don't be mistaken into thinking that we couldn't help you. In contrast, we actually know exactly how your code should look to work perfectly, It's just that we're trying to get you to see why what you've posted doesn't work at all, so you'll know what to fix.

As it stands, your descending order code is actually a tiny bit closer to working than your ascending order code, because it doesn't have the glaring mistake of line 14. It's still not a working sorting algorithm though. Don't be fooled into thinking that one right output from the program means that everything is perfectly correct and working in any other case.