# sorting a structure of arrays

Printable View

• 03-14-2002
Unregistered
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);
• 03-14-2002
quzah
Don't you mean "an array of structures"?

Quzah.
• 03-14-2002
kdturkay
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.
• 03-14-2002
kdturkay
i'm sorry that i wrote a structure of arrays above
• 03-14-2002
Prelude
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
• 03-15-2002
Unregistered
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);
• 03-15-2002
Sayeh
Why don't you use the built-in function qsort()? This is what it is for? It's very simple to use...
• 03-15-2002
bluebob
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?