Thread: bubsort working for one text file but not sorting the second file properly

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    2

    bubsort working for one text file but not sorting the second file properly

    I have a program to read in a set of data from a file, and sort the data in alphabetical order. I use bubsort and within bubsort i use strcmp. When i open file one (5 entries) the program runs fine and all data is sorted alphabetically. However when I open the second file (21 entries) the sorting is not in alphabetical order.
    My question is if it works for one file why is it not working for the second? I am beginner programmer.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    2
    this is my bubsort code:-
    The data being read from the files is of the following form

    surname, forename grade
    surname1, forename1 grade1
    surname2, forename2 grade2
    etc........

    Code:
    /** BUBBLESORT ALGORITHM **/
     
    void bubble_sort (student *student_data, int entries)
     
    {
     
    int j, k;
    Boolean exchange_made;
    student temp;
    k = 1;
    exchange_made = TRUE;
    int n=entries;
     
    /* Make up to (n - 1) passes through array, exit early if no exchanges are made on previous pass */
    
      while ((k <= entries) && exchange_made)
       {
          exchange_made = FALSE;
                   
           for (j = 0; j < n-1; ++j)
           {
             if (strcmp(student_data[j].surname,student_data[j+1].surname)>0)
              {
                  /* Number of comparisons on kth pass */
    
                  temp = student_data[j];
                  student_data[j] = student_data[j + 1];
                  student_data[j + 1] = temp;
                  exchange_made = TRUE;
     
                  /* Exchange must be made*/
               }
    
               k++;         
            }
    }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't see why the k variable is needed, at all. Scrap it, and the first part of the while test. When no exchanges are made, THAT's all you need.

    Give that a try.

    If you're using the same array, did you clear out the first array's data or know that your number of entries is accurate?

    Describe the output you're getting with the second sort?

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Agreed, scrap the (k <= entries) && test and the k++;. The only time it can ever require the maximum number of passes is when the smallest value is initially in the last position. In every other case the check is just more unnecessary work. Besides if you were really caring about optimisation, there are far better changes to make.
    In fact, the position of the incrementing of k is also the reason why it is failing. You're very lucky it happened to work for when there are only 5 items as it could just as easily fail with 5 items starting in a different order.
    I was already going to write this before I saw Adak's post, so there you have two independent responses for scrapping the k part.

    Also, as you appear to be compiling this with a C++ compiler, why not just use the standard bool instead of a custom Boolean?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting a simple text file
    By napalm in forum C Programming
    Replies: 4
    Last Post: 03-25-2008, 02:43 PM
  2. Help with sorting a text file database
    By bds824 in forum C Programming
    Replies: 5
    Last Post: 02-14-2006, 11:58 PM
  3. Text file sorting help
    By KellyWhite1 in forum C++ Programming
    Replies: 1
    Last Post: 01-05-2006, 04:53 PM
  4. Help sorting a text file
    By cuddlez.ini in forum C++ Programming
    Replies: 4
    Last Post: 02-25-2005, 09:11 AM
  5. Sorting text file problem...
    By John-m in forum C Programming
    Replies: 3
    Last Post: 10-01-2002, 04:51 PM