Selection Sort incorrectly switched last 2 indexes

I have been working on this program using classes and a couple of parallel arrays however I have a sort method that continues to switch the last two indexes when what I want it to do is just sort in descending order.

Here is the sort method:

void Sort()
{
for (int i = 0; i <= numStars; i++)
{
int mindex = i;
for (int j = i + 1; j < numStars; j++)
{
if (Distance(mindex) < Distance(j))
mindex = j;
double tempValue;
int tempInt;
tempInt = starID[mindex];
starID[mindex] = starID[i];
starID[i] = tempInt;
tempValue = period[mindex];
period[mindex] = period[i];
period[i] = tempValue;
tempValue = apparentMag[mindex];
apparentMag[mindex] = apparentMag[i];
apparentMag[i] = tempValue;
}
}
}

An example for the output is my program outputs this:

Stars for Observatory with ID 6325:
Apparent Absolute Extinction
StarID period Magnitude Magnitude Distance IsHydra Distance
------------------------------------------------------------------------------
65516 0.65 21.29 0.38 151767.53 Y 148560.20
44108 0.60 16.57 0.42 17003.07 N 16152.92
9496 0.34 15.86 0.68 10878.08 N 10334.18
10426 0.60 14.99 0.41 8227.63 N 7816.25
4763 0.66 17.84 0.38 31115.21 N 29559.45
2447 0.54 17.60 0.47 26691.27 N 25356.71
Sorted by distance for observatory 6325.
Stars for Observatory with ID 6325:
Apparent Absolute Extinction
StarID period Magnitude Magnitude Distance IsHydra Distance
------------------------------------------------------------------------------
65516 0.65 21.29 0.38 151767.53 Y 148560.20
2447 0.54 17.60 0.47 26691.27 N 25356.71
4763 0.66 17.84 0.38 31115.21 N 29559.45
44108 0.60 16.57 0.42 17003.07 N 16152.92
9496 0.34 15.86 0.68 10878.08 N 10334.18
10426 0.60 14.99 0.41 8227.63 N 7816.25

When it should be this:

Stars for Observatory with ID 6325:
Apparent Absolute Extinction
StarID period Magnitude Magnitude Distance IsHydra Distance
------------------------------------------------------------------------------
65516 0.65 21.29 0.38 151767.53 Y 148560.20
44108 0.60 16.57 0.42 17003.07 N 16152.92
9496 0.34 15.86 0.68 10878.08 N 10334.18
10426 0.60 14.99 0.41 8227.63 N 7816.25
4763 0.66 17.84 0.38 31115.21 N 29559.45
2447 0.54 17.60 0.47 26691.27 N 25356.71
Sorted by distance for observatory 6325.
Stars for Observatory with ID 6325:
Apparent Absolute Extinction
StarID period Magnitude Magnitude Distance IsHydra Distance
------------------------------------------------------------------------------
65516 0.65 21.29 0.38 151767.53 Y 148560.20
4763 0.66 17.84 0.38 31115.21 N 29559.45
2447 0.54 17.60 0.47 26691.27 N 25356.71
44108 0.60 16.57 0.42 17003.07 N 16152.92
9496 0.34 15.86 0.68 10878.08 N 10334.18
10426 0.60 14.99 0.41 8227.63 N 7816.25

Thank you for any help you have!
Use code tags when you post code.
https://www.cplusplus.com/articles/jEywvCM9/

Also, as it's probably apparent that your code isn't indented properly to begin with.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void Sort()
{
  for (int i = 0; i <= numStars; i++) {
    int mindex = i;
    for (int j = i + 1; j < numStars; j++) {
      if (Distance(mindex) < Distance(j))
        mindex = j;
      double tempValue;
      int tempInt;
      tempInt = starID[mindex];
      starID[mindex] = starID[i];
      starID[i] = tempInt;
      tempValue = period[mindex];
      period[mindex] = period[i];
      period[i] = tempValue;
      tempValue = apparentMag[mindex];
      apparentMag[mindex] = apparentMag[i];
      apparentMag[i] = tempValue;
    }
  }
}

The scope of line 6 is just line 7.
The rest of the swapping code just happens anyway, regardless of your comparison.
Maybe you're missing some braces?

https://www.cplusplus.com/reference/utility/swap/
So you can write
1
2
std::swap( starID[mindex], starID[i] );
// etc 

Referring to salem c's code, line 3 appears to be causing you to index out of range.
You didn't post the code for the Distance() function, so I have to assume it refers to an array of 0 - (numStars-1) values. Using <= will cause the last iteration to set i to numStars, which is one past the end of the array.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Topic archived. No new replies allowed.