Thread: Code not sorting the way I would like...

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    6

    Code not sorting the way I would like...

    Hi all. I am receiving some tutoring this summer, and I'm having a bit of an issue with an assignment. I created a program that reads from a text file, and I'm trying to have the program go in ascending order from an object in my class. I'm trying to have it read the GPAs entered from the text file be what gives the order it's printed on screen. I am using the quicksort algorithm to try to do the sorting. Here's my code for the quicksort (I know it's wrong)

    Code:
    void QUICKSORT(double GPA [], int l, int r)
    {
        int i, j, t, p;
        i = l;
        j = r;
        p = GPA[(l+r)/2];
    
    
        while(i <= j)
        {
            while (GPA[i] < p)
            i++;
            while(GPA[j] > p)
                j--;
    
    
            if(i <= j)
            {
                t = GPA[i];
                GPA[i] = GPA[j];
                GPA[j] = t;
                i++;
                j--;
            }
        }
    
    
        if(l < j)
            QUICKSORT(GPA, l, j);
    
    
        if(i < r)
            QUICKSORT(GPA, i, r);
    
    
    };
    Here is what the class looks like:

    Code:
    class Students{
    
    
    private:
    
    
        string Name;
        int ID;
        string Major;
        double GPA;
        string Grade;
    
    
    public:
    
    
        string getName(){
    
    
        return Name;
    
    
        }
    
    
        void setName(string x){
    
    
        Name = x;
    
    
        }
    
    
        int getID(){
    
    
        return ID;
    
    
        }
    
    
        void setID(int x){
    
    
        ID = x;
    
    
        }
    
    
        string getMajor(){
    
    
        return Major;
    
    
        }
    
    
        void setMajor(string y){
    
    
        Major = y;
    
    
        }
    
    
    
    
        double getGPA(){
    
    
        return GPA;
    
    
        }
    
    
    
    
        void setGPA(double x){
    
    
        GPA = x;
    
    
        }
    
    
        string getGrade(){
    
    
        return Grade;
    
    
        }
    
    
        void setGrade (string z){
    
    
        Grade = z;
    
    
        }
    
    
            void printInfo(){
    
    
            cout << "Student Name: " << Name << endl;
            cout << "ID Number: " << ID << endl;
            cout << "Major: " << Major << endl;
            cout << "Current GPA: " << GPA << endl;
            cout << "Class Rank: " << Grade << endl << endl;
        }
    The file compiles, and does print. However, it's not going in the order that I need it to go in. I know I need to have the Quicksort function called in the int main(), but anywhere I put it, it wouldn't work. What am I doing wrong here????

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    The thing is, you don't have an array of GPA's.
    You have an array of students (presumably), each containing a single GPA.

    So perhaps
    Code:
    void QUICKSORT(Students students[], int l, int r) {
      double p = students[(l+r)/2].getGPA();
      // etc
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with sorting code
    By kalyan12 in forum C Programming
    Replies: 1
    Last Post: 02-28-2014, 12:29 AM
  2. Need help with my Sorting code.
    By John120788 in forum C Programming
    Replies: 8
    Last Post: 07-12-2013, 04:06 AM
  3. Zip Code Sorting
    By Shamino in forum C++ Programming
    Replies: 14
    Last Post: 10-26-2009, 04:20 PM
  4. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  5. Sorting Linked Lists (code not concept)
    By Newbie Magic in forum C++ Programming
    Replies: 2
    Last Post: 05-11-2004, 08:57 AM

Tags for this Thread