Thread: sorting a structure of arrays

1. sorting a structure of arrays

i am trying to sort a structure of arrays by catagory. But it doesnt seem to be working, any ideas on where its going wrong?

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].videocat;
VideoData[x-1].videocat = VideoData[x].videocat;
VideoData[x].videocat = 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. Don't you mean "an array of structures"?

Quzah.

3. your following three lines are wrong

tmp = VideoData[x-1].videocat;
VideoData[x-1].videocat = VideoData[x].videocat;
VideoData[x].videocat = tmp;

while you are sorting a structure of arrays, you must change places of all structure.
the right lines :

tmp = Videodata[x-1];
VideoData[x-1] = VideoData[x];
VideoData[x] = tmp;

that's all you have to do.

4. i'm sorry that i wrote a structure of arrays above

5. Ew, bubble sort.

>while you are sorting a structure of arrays, you must change places of all structure
No, please don't do this. If the array is large and the structures have many members then you'll end up shuffling more data in memory than you want to. To keep your program efficient (somewhat), use two arrays. One is your array of structures and the other is an array of pointers to each element of your array of structures. Sort the array of pointers by dereferencing the members that you want to sort by. The two arrays would look something like this:
Code:
```Pointers   Structures
--------   ----------
4 byte --> 80 byte
4 byte --> 80 byte
4 byte --> 80 byte
4 byte --> 80 byte
4 byte --> 80 byte
4 byte --> 80 byte
4 byte --> 80 byte
4 byte --> 80 byte
4 byte --> 80 byte
4 byte --> 80 byte```
Would you rather sort the pointers, or the structures? Same here. I don't know the size of your struct, but the point still stands, it's better to shuffle small bits of data than to shuffle large amounts.

This will give you extra flexibility and efficiency since you will only be sorting 4 byte (not always though) elements instead of god knows how many byte elements. And since bubble sort is so crappy anyway you need all the efficiency you can get.

-Prelude

6. Ive amended my code, and it is givng me these errors-
Videodata' : undeclared identifier
subscript requires array or pointer type
'=' : incompatible types

any ideas?

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);

7. Why don't you use the built-in function qsort()? This is what it is for? It's very simple to use...

8. We have been told to use bubble sort, as it is the easiest (his words, not mine).

How would i implemenet a qsort on an structure of arrays anyway?