Thread: sorting a structure of arrays

  1. #1
    Unregistered
    Guest

    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. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Don't you mean "an array of structures"?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    kdturkay
    Guest
    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. #4
    kdturkay
    Guest
    i'm sorry that i wrote a structure of arrays above

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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
    My best code is written with the delete key.

  6. #6
    Unregistered
    Guest
    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. #7
    Sayeh
    Guest
    Why don't you use the built-in function qsort()? This is what it is for? It's very simple to use...

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    22
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Zeroing out member arrays in a Structure
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 03-26-2004, 03:50 AM
  2. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  3. Question sorting character arrays: C programming
    By NeoSigma in forum C Programming
    Replies: 3
    Last Post: 05-23-2003, 09:28 PM
  4. Sorting STRING arrays
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 06-21-2002, 12:39 PM
  5. Sorting Arrays
    By Jax in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 12:35 PM