1

How do I sort two array's of of structs(one of the arrays is a member of the other struct) with the same function using bubble sort (Descending order for students[] on Student::name and sort the classes[] array on Class::title?

struct Class
{
    string title; 
    int units;
    char grade;

};
struct Student
{
    string name;
    double gpa;
    Class classes[500];
};

In main:

Student students[SIZE];

I am attempting to sort an array of structs that each contain an array of structs that also need to be sorted using bubble sort. My sorting function is pasted below. It does not sort correctly rather it sorts the inner array of structs classes[] according to title correctly, and sorts the outer array st[] correctly on the first iteration of the for loop. Since on the second iteration the elements of st[] have been swapped the first element doesn't get sorted b/c currentStu is now set to the second element in the array.

void sort_name(Student st[], int numValues)
{
  int currentStu = 0;
  int currentClass = 0;

  for(currentStu = 0; currentStu < numValues; currentStu++)
  {
        for(currentClass = 0; st[currentStu].classes[currentClass].title != ""; currentClass++)
        {
            bubbleUpClass(st, currentClass, currentStu);
        }

        bubbleUpLastName(st, currentStu, numValues - 1);
  }
}
5
  • I pity the poor student who actually takes 500 classes! Commented Nov 2, 2012 at 23:57
  • LOL, I am trying to finish school as quickly as I can (5-6 classes per quarter). So this is how I felt when I was coding this :) Commented Nov 2, 2012 at 23:59
  • Considering a 9-year doctorate program would take (5classes/term * 3terms/year * 9years) would be at most 145 classes, I'm seriously wonder what this student is studying, and how big a lottery they need to win to pay off their student loans. Commented Nov 3, 2012 at 0:01
  • @JonathanLeffler thanks; I've been staring at code way too long today. 135 classes. (*don't tell me I f'ed that one up too;) Commented Nov 3, 2012 at 0:08
  • Unless your bubbleUpStudent has a hole in its algorithm the student that should be in slot[0] of students should home after your first pass; in slots[0..1] after two passes, etc. is your bubble function pushing items to the end of the table? if so, it may be backwards. It is also possible your invoke should be bubbleUpLastName(st, currentStu, numValues - currentStu); Commented Nov 3, 2012 at 0:12

1 Answer 1

2

You don't really have a 2D array of students, which is (on the whole) a good thing. You need to apply two separate sorting processes, and they can be applied quite independently.

  1. You need to iterate through your list of students, sorting each of the lists of classes (one per student). It is not clear how you know how many classes a given student is taking, but that's a problem for you to solve. You can do this before or after (but not during) the other sort operation. It is readily parallelizable if that was of interest; you could divide the list of students up amongst N threads, giving each thread a suitable set of students to work on.

  2. You need to sort your overall list of students. This operation will affect the entire array of students (or, at least, the populated part of it). You will do this sort either before or after (but not during) the other sort operation.

You will need two separate sort functions — or, if you borrow the design of the Standard C function qsort(), you will two separate comparator functions and a single sort algorithm.

So, don't try to combine the two sort operations. Do them separately.

Sign up to request clarification or add additional context in comments.

2 Comments

Separating the sorting processes would make life a lot easier, unfortunately this is an assignment and it requires the sorting to be done in one function.
Said assignment is stupid! What counts as one function? void sort_this(Student *s, size_t n) { for (size_t i = 0; i < n; i++) sort_class(s[i]); for (size_t i = 0; i < n-1; i++) { ...sort s... } }? Where you can write the sort algorithm labelled sort_class() inline in the function? If you've got to do it in one function, then you can, but you should write the properly engineered solution, and then butcher it so it meets the requirements of the assignment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.