Sorting array of structs by letter grade

Solved
Last edited on
What is wrong with your current sort routine?

I recommend you move the "printing" out of the sort function and just sort the array in the sort function, print the array elsewhere.

Also be careful with your function parameter names. Naming a parameter the same as a global variable hides the global variable.

You may want to double check your sort routine, it doesn't look like it's swapping the array elements correctly.



Solved
Last edited on
I think the problem is with the minCount variable. It is set to the value of i and at the exchange part (line 186 to 187) you aren't really exchanging, because i and minCount are indexing the same object. I think the exchange part should be
1
2
3
temp = ptr[i];
ptr[i] = ptr[j];
ptr[j] = temp;


I think you should also get rid of minCount since it serves the same purpose as i
I think the problem is with the minCount variable. It is set to the value of i and at the exchange part (line 186 to 187) you aren't really exchanging, because i and minCount are indexing the same object. I think the exchange part should be

1
2
3
temp = ptr[i];
ptr[i] = ptr[j];
ptr[j] = temp;



I think you should also get rid of minCount since it serves the same purpose as i

The thing with that is if I used ptr[j] to swap it, that's outside of the inner for loop and therefore j would be uninitialized.
Looking at some documentation for a selection sort your sort is not quite correct. You may want to review your documentation for this sort. Here is the link I used:

https://www.tutorialspoint.com/data_structures_algorithms/selection_sort_algorithm.htm

Your "swap" should probably be in an if() statement inside the outer for() loop.

This is what I came up with using the above documentation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void selectionSortArray(Student students[], int array_size)
{
    Student temp;

    for(int i = 0; i < (array_size - 1); i++)
    {
        int position = i;

        for(int j = i + 1; j < array_size; j++)
        {
            if(students[position].letterGrade > students[j].letterGrade)
            {
                position = j;
            }
        }

        if(position != i)
        {
            temp = students[i];
            students[i] = students[position];
            students[position] = temp;
        }
    }
}


Looking at some documentation for a selection sort your sort is not quite correct. You may want to review your documentation for this sort. Here is the link I used:

https://www.tutorialspoint.com/data_structures_algorithms/selection_sort_algorithm.htm

Your "swap" should probably be in an if() statement inside the outer for() loop.

This is what I came up with using the above documentation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void selectionSortArray(Student students[], int array_size)
{
    Student temp;

    for(int i = 0; i < (array_size - 1); i++)
    {
        int position = i;

        for(int j = i + 1; j < array_size; j++)
        {
            if(students[position].letterGrade > students[j].letterGrade)
            {
                position = j;
            }
        }

        if(position != i)
        {
            temp = students[i];
            students[i] = students[position];
            students[position] = temp;
        }
    }
}


I had played with it a little bit and came up with the following code which worked:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void selectionSortArray(Student *ptr, int NUM_STUDENTS)
{
   Student temp;
   char min_pos;

   for (int i = 0; i < (NUM_STUDENTS - 1); i++)
   {
      min_pos = i + 1;
      for (int j = i + 1; j < NUM_STUDENTS; j++)
      {
         if (ptr[j].letterGrade < ptr[min_pos].letterGrade)
         {
            min_pos = j;
         }
      }

      temp = ptr[min_pos];
      ptr[min_pos] = ptr[i];
      ptr[i] = temp;
   }
   cout << endl;
}

@jlb, Thank you very much your code works as well. I'm still trying to understand each step but I really appreciate it!
Last edited on
@slimdog Why did you delete the contents of your earlier posts? That makes this thread useless as a learning resource for the other users of this forum.

Please edit your posts to reinstate their original contents, so that this can be read and understood by others.
Topic archived. No new replies allowed.