简体   繁体   中英

C++ Selection Sort on Array of Structures

I am working through a C++ book, and I am a little stuck on one of the challenge questions. I'm working on learning about pointers, and in this particular problem I need to sort an array of structures (using pointers) with a string of a student's name and a double of their score. After the sort, the structure's data members still need to match up, obviously (ie the right name's still need to be with their scores).

This is where my problem lies. So far, I have the sort properly arranging the scores in ascending order, but the names get all jumbled up. I haven't been able to figure out why, partly because I am still working to fully understand pointers and how to use them. I can do a bubble sort correctly, keeping the names with their scores, but not the selection sort. Any help would be greatly appreciated.

Here is the function I have for the selection sort:

void selection_sort(Student *ptr, int size) // selection sort - having some problems
{
  int start,
    min_index,
    min_value;

  for (start = 0; start < (size - 1); start++) {
    min_index = start;
    min_value = (ptr+start)->score;
    for (int index = start+1; index < size; index++) {
      if ( (ptr+index)->score < min_value) {
    min_value = (ptr+index)->score;
    min_index = index;
      }
    }
    // the following line is where, i think, the problem is, but i haven't
    // been able to figure out the solution, despite trying numerous approaches
    *(ptr+min_index) = *(ptr+start);
    (ptr+start)->score = min_value;
  }
}

So that is what I have. I'm not great with sorting algorithms yet either, and this is all pretty new to me, so I hope it's not horribly messed up. If anyone knowledgeable in these areas could point me in the right direction, that would be awesome.

First of all I would like to give you one tip: instead of using the syntax *(ptr+min_index) you can use ptr[min_index] and it will have the same effect. I believe this version is more natural.

Second - your problem. You should swap ptr[min_index] and ptr[start] rather then just copying the values of one of them to the other. That is instead of:

*(ptr+min_index) = *(ptr+start);
(ptr+start)->score = min_value;

Write this:

Student temp = ptr[start];
ptr[min_index] = ptr[start];
ptr[start] = temp;

Or if you are using c++ simply use the swap function:

std::swap(ptr[min_index], ptr[start]);

Why should you swap instead of what you are currently doing? Well, you should preserve all the fields in ptr[min_index] in order to be able to assign them to ptr[start].

Hope this helps.

I think you should use memcpy function in the standard library...

And one more thing:

*(ptr+min_index) = *(ptr+start); 

This line seems to overwrite the data, but NOT swap them as they should be.

First lesson in C++ : In C++ we have operator overloading, so the line like this:

 *(ptr+min_index) = *(ptr+start); 

can have meaning if your Student class has any pointer in his member attributes.

and you must use a swap and not just assign.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM