# Sorting problem?

Printable View

• 01-05-2009
audinue
Sorting problem?
Code:

```#include <stdio.h> #include <stdlib.h> typedef struct {   int id;   const char *data; } Test; int main() {   Test **array = malloc(3 * sizeof(Test *));   (*(array)) = malloc(sizeof(Test));   (*(array))->id = 3;   (*(array))->data = "3";   (*(array + 1)) = malloc(sizeof(Test));   (*(array + 1))->id = 2;   (*(array + 1))->data = "2";     (*(array + 2)) = malloc(sizeof(Test));   (*(array + 2))->id = 1;   (*(array + 2))->data = "1";   int i;   for(i=0; i<3; i++) {     printf("%s ", (*(array + i))->data);   }     for(i=1; i<3; i++) {     int j;     for(j=i; j>-1; j--) {       if(((*(array + i))->id) < ((*(array + j))->id)) {         Test *tmp = *(array + i);         *(array + i) = *(array + j);         *(array + j) = tmp;       }     }   }   printf("\n");   for(i=0; i<3; i++) {     printf("%s ", (*(array + i))->data);   }   return 0; }```
Can anyone guess?
Is there something wrong with its algorithm or code?

The output should be: 1 2 3
But it is: 2 1 3

?
• 01-05-2009
anon
You seem to malloc more than needed.

First, what's wrong with:
Code:

`Test array[3];`
If, for some reason you need to allocate the array dynamically, why not
Code:

`Test* array = malloc( 3 * sizeof(Test) );`
The output also seems to indicate that the array is not sorted correctly. Look up bubble sort and don't try to be too clever (bubble-sort compares items that are next to each other and runs over the array (or part of it) as many times as needed.
• 01-05-2009
Adak
In your sorting loop, it looks like you're starting at element 1, not zero, so it doesn't get sorted.

I'd recommend using this for your loops:

Code:

```for(i = 0; i < 3 - 1; i++)  {   for(j = i + 1; j < 3; j++)  {       if(...       //do all your comparisons and swaps as needed   } } //this is selection sort (very close to a bubble sort), and guaranteed to work right //if your comparison and swaps are right.```
• 01-05-2009
audinue
Quote:

Originally Posted by anon
You seem to malloc more than needed.

First, what's wrong with:
Code:

`Test array[3];`
If, for some reason you need to allocate the array dynamically, why not
Code:

`Test* array = malloc( 3 * sizeof(Test) );`
The output also seems to indicate that the array is not sorted correctly. Look up bubble sort and don't try to be too clever (bubble-sort compares items that are next to each other and runs over the array (or part of it) as many times as needed.

Quote:

Originally Posted by Adak
In your sorting loop, it looks like you're starting at element 1, not zero, so it doesn't get sorted.

I'd recommend using this for your loops:

Code:

```for(i = 0; i < 3 - 1; i++)  {   for(j = i + 1; j < 3; j++)  {       if(...       //do all your comparisons and swaps as needed   } } //this is selection sort (very close to a bubble sort), and guaranteed to work right //if your comparison and swaps are right.```

So, the problem was its algorithm :D thanks Adak.

anon: I'm practicing my pointer logic. It seems that people in sf.net love using pointers so that I can't read their source code :D Thanks for reply anyway.
• 01-05-2009
iMalc
Quote:

Originally Posted by audinue
Ugly C Lover

Confirmed:
Quote:

Originally Posted by audinue
Code:

`  (*(array + 1)) = malloc(sizeof(Test));`

Most people prefer "array[1] = ...";
• 01-06-2009
audinue
Quote:

Originally Posted by iMalc
Confirmed:

Most people prefer "array[1] = ...";

Yay, thats too C :D