Thread: My Bubble Sort, help

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

    My Bubble Sort, help

    This sort seems to work the first time it is run, but if i run it a second time, it sorts the array in the opposite direction, and so on. 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;
    }

    // then print statements

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    87
    Here's some code I wrote in java a while ago, the idea is the same for C and C++, except that i'm using vectors here.

    Code:
    public int bubbleSort() {
       String temp;
       for(int i=0; i < current_size; i++) { 
          for(int j=i+1; j < current_size; j++) {
             if(lessThan((String)data.elementAt(j),
                (String)data.elementAt(i))) {
                temp = (String)data.elementAt(j);
                data.setElementAt(data.elementAt(i), j);
                data.setElementAt(temp, i);
             }//end if
          }//end for
       }//end for
       return current_size;
    }//end bubbleSort
    PuterPaul.co.uk - Portfolio site

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bubble sort not working... wats d prob?
    By Huskar in forum C Programming
    Replies: 8
    Last Post: 03-31-2009, 11:59 PM
  2. My bubble sort only sorts once
    By Muller in forum C Programming
    Replies: 8
    Last Post: 03-27-2009, 04:36 PM
  3. Bubble Sort... which type?
    By gflores in forum C++ Programming
    Replies: 8
    Last Post: 08-15-2004, 04:48 AM
  4. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 04:54 PM
  5. Help with Bi-Directional Bubble Sort in C
    By cunninglinguist in forum C Programming
    Replies: 0
    Last Post: 04-19-2002, 02:32 PM