bubble sort and structures?

This is a discussion on bubble sort and structures? within the C Programming forums, part of the General Programming Boards category; This bubble sort isnt working for me, any ideas? Im trying to sort a structure of arrays by one variable ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    22

    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. #2
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    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.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    22
    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. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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
    My best code is written with the delete key.

  5. #5
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59
    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. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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
    My best code is written with the delete key.

  7. #7
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59
    Prelude ... i think i understand ...

    /* my code snippet

    int *fment = array;

    int *sment = &array[1];

    */

    is not the same as

    /* your code snippet

    int fment = &array[0];

    int sment = &array[1];

    */

    however why isnt ...

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

    curious

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bubble sort
    By nynicue in forum C Programming
    Replies: 7
    Last Post: 04-15-2009, 06:09 AM
  2. Anyone knows how to sort arrays of structures?
    By sherwi in forum C Programming
    Replies: 19
    Last Post: 04-30-2006, 06:52 PM
  3. Array/Structure Bubble Sort
    By JKI in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2003, 12:59 PM
  4. Sorting an array of structures with sort()
    By rmullen3 in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2003, 03:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21