# Thread: bubble sort and structures?

1. ## bubble sort and structures?

This bubble sort isnt working for me, any ideas? Im trying to sort a structure of arrays by one variable in structure. Im trying to sort by videocat.

for (j = 0; j < i - 1; j++)
for (x = i -1; x > j; --x)
if (VideoData[x-1].videocat < VideoData[x].videocat)
{
tmp = VideoData[x-1];
VideoData[x-1] = VideoData[x];
VideoData[x] = tmp;
}

for (j = 0; j < i - 1; j++)
printf("%-9.9s %-16.16s %-12.2s %-10s %-14.2f %c\n", VideoData[j].videonumber, VideoData[j].videotitle, VideoData[j].videocert, VideoData[j].videocat, VideoData[j].dailyrental, VideoData[j].available);

2. Code:
```for (j = 0; j < i - 1; j++)
for (x = i -1; x > j; --x)```
Why do you have 2 for loops? You do not need them.

3. Help. I just cant this code to work. I can enter the data into the array, and i know its there, because i can display all items in the structure of arrays, and it displays everything fine.

But when it comes to sorting by catagory, and displaying, i just cant gte it to work. It just displays the contents, but they are not sorted. Can anyone help?

for (j = 1; j > i; j++)
if (VideoData[j-1].videocat < VideoData[j].videocat)
{
tmp = VideoData[j-1];
VideoData[j-1] = VideoData[j];
VideoData[j] = tmp;
}

for (j = 0; j < i; j++)
printf("%-9.9s %-16.16s %-12.2s %-10s %-14.2f %c\n", VideoData[j].videonumber, VideoData[j].videotitle, VideoData[j].videocert, VideoData[j].videocat, VideoData[j].dailyrental, VideoData[j].available);

4. Scrap your code, it's too complex right now for you to be solving problems with sorting. Break the program down into a little test program that does nothing but sort the array of structures. Once you have a bare bones program that you can work with, step through the code line by line and debug it by checking values. I hate bubble sort, but if you must use it here is some template code that you can base yours off of.
Code:
```/* pseudocode */
void bSort ( struct T *a, int min, int max )
{
int i, j;
struct T temp;
for ( i = min; i < max; i++ )
for ( j = max; j > i; j-- ) {
if (a[j-1].cat < a[j].cat) {
temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
}
}
}```
That code should work, but I didn't test it so don't rely on the exact code as much as the algorithm

-Prelude

5. since we are on the Bubble sort topic i was wondering if anyone could tell me why i cannot get a Bsort to work with pointers ?

here is my mess;

int test(int *array)
{
int *fment = array;
int *sment = &array[1];
int temp, count1, count2;

for(count2=0;count2 < MAXSIZE -1;count2++)
{
for(count1=0;count1 < MAXSIZE -1;count1++, fment++,sment++)
{
if(*sment < *fment)
{
temp = *fment;
*fment = *sment;
*sment = temp;
}

}

}

return 0;

}

thanks for any help that i migth recieve

6. >here is my mess
Yes
Code:
```int test(int *array)
{
int *fment, *sment;
int temp, count1, count2;
for(count2=0;count2 < MAXSIZE;count2++) {
sment = &array[1];
fment = &array[0];
for(count1=0;count1 < MAXSIZE-1;count1++, fment++,sment++) {
if(*sment < *fment) {
temp = *fment;
*fment = *sment;
*sment = temp;
}
}
}
return 0;
}```
-Prelude

7. Prelude ... i think i understand ...

/* my code snippet

int *fment = array;

int *sment = &array[1];

*/

is not the same as

int fment = &array[0];

int sment = &array[1];

*/

however why isnt ...

*fment = array
the same as
fment = &array[0]

curious

8. There is no difference, I changed it that way so that it was more obvious that you were assigning the first element and not the entire array to the pointer. Using code to describe your intentions is always a good idea.

The biggest difference in how I changed the function was when I gave the values to your pointers. Notice that I gave your pointers the value of the first and second elements for every iteration of the outer loop. Play around with it and see what other changes I made and try to figure out why.

-Prelude