简体   繁体   English

C ++选择按结构数组排序

[英]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. 我正在阅读一本C ++书,但是我对其中一个挑战性问题有些困惑。 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. 首先,我想给您一个提示:您可以使用ptr[min_index]而不是使用语法*(ptr+min_index)来达到相同的效果。 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. 您应该交换ptr[min_index]ptr[start]而不是仅将其中一个的值复制到另一个。 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: 或者,如果您使用的是c ++,则只需使用swap函数:

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]. 好吧,您应该保留ptr[min_index]中的所有字段,以便能够将它们分配给ptr [start]。

Hope this helps. 希望这可以帮助。

I think you should use memcpy function in the standard library... 我认为您应该在标准库中使用memcpy函数...

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: C ++的第一课:在C ++中,我们有运算符重载,因此如下所示:

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

can have meaning if your Student class has any pointer in his member attributes. 如果您的Student类的成员属性中有任何指针,则可能具有含义。

and you must use a swap and not just assign. 并且您必须使用交换,而不仅仅是分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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