Selection Sort
Mar 11, 2019 at 4:33am UTC
I have this function that is supposed to organize names along with corresponding scores in descending order using selection sort. The scores will go descending order but the corresponding names do not, and I am trying to figure out why?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
void sortData(HighScore scores[], int size)
{
int maxIndex, maxValue;
string nameWithmaxValue;
for (int startScan = 0; startScan < (size - 1); startScan++) {
maxIndex = startScan;
maxValue = scores[startScan].scores;
nameWithmaxValue = scores[startScan].name;
for (int index = startScan + 1; index < size; index++) {
if (scores[index].scores > maxValue) {
swap(scores[index].scores, scores[index].scores);
maxValue = scores[index].scores;
nameWithmaxValue = scores[index].name;
maxIndex = index;
}
}
scores[maxIndex].scores = scores[startScan].scores;
scores[startScan].scores = maxValue;
*scores[maxIndex].name = *scores[startScan].name;
//*scores[startScan].name = nameWithmaxValue;
}
I have also tried calling swap just like above but with .name instead of .scores and that didn't work either.
Last edited on Mar 11, 2019 at 4:36am UTC
Mar 11, 2019 at 8:39am UTC
swap( scores[maxIndex], scores[index] );
(assuming that you are using std::swap from <algorithm>
Topic archived. No new replies allowed.