0

So I have my selection sort sorting a list of a student array of structs on the student ID number. The issue I am having is that it isn't sorting just the last two of each file, and I can't figure out what is wrong.

void assortList(STUDENT* list, int size)
{
    int startScan;
    int minIndex;
    int minValue;

    for(startScan = 0; startScan < (size - 1); startScan++)
    {
        minIndex = startScan;
        minValue = list[startScan].ID;
        for(int index = startScan + 1; index < size; index++)
        {
            if(list[index].ID < minValue)
            {
                minValue = list[index].ID;
                minIndex = index;
            }
            STUDENT temp = list[minIndex];
            list[minIndex] = list[startScan];
            list[startScan] = temp;
        }
    }

    for(int x = 0; x < size; x++)
    {
        FLUSH;
        printf("%d %s\n", list[x].ID, list[x].name);
    }   

    printf("\n");
}

Output

List 1:

1189 Shmoys, David
1234 Marley, Tom
2901 Green, Mary
2908 Vigoda, Eric
3456 Karlin, Anna
4344 Kelley, Sandra
5445 Homer, Steve
5567 Welch, Jennifer
6566 Williams, Ryan
6579 Vadhan, Salil
8372 Chen, Li
8879 Bein, Wolfgang
8999 Fenner, Mia
9002 Khuller, Samira
9123 Vianu, Victor
9865 Beame, Paul
6766 Hemaspaandra, Lane
8433 Chakrabarti, Amit

List 2

1111 Tan, Li-Yang
2000 Barenboim, Leonid
2001 Rossman, Marie
3456 Karlin, Anna
4344 Kelley, Sandra
5445 Homer, Steve
5511 Welch, Claire
6577 Green, Susan
8433 Chakrabarti, Amit
8800 Servedio, Rocco
8999 Fenner, Mia
9123 Vianu, Victor
9865 Beame, Paul
6009 Mumey, Brendan
6666 Forbes, Michael

You'll notice from the output, that it is just the last two students of each list (6766, 8433 in the first set; 6009 and 6666 in the second set) that are not being sorted.

2
  • You show some output; that's good. You don't show the input (so we can have no idea which are or are not sorted), nor do you show the code that calls the function. It isn't clear, therefore, where the trouble is. It may not be in the code you do show. For example, if your counting is off, you may be telling the sort to sort too few rows. This could happen if your counter contains the last index assigned in the array, and if you also subtract one before calling the sort. Not particularly likely, but possible — and impossible for us to help because you've not shown it. Commented Jan 12, 2016 at 5:16
  • @JonathanLeffler Moving the last section out one bracket to the first for loop did it. Sorry about that and next time I will be sure to post sufficient code. Thanks. Commented Jan 12, 2016 at 5:51

1 Answer 1

1

Corrected code:

for (startScan = 0; startScan + 1 < size; ++startScan) {
  minIndex = startScan;
  minValue = list[startScan].ID;
  for (int index = startScan + 1; index < size; ++index) {
    if (list[index].ID < minValue) {
      minValue = list[index].ID;
      minIndex = index;
    }
  }
  STUDENT temp = list[minIndex];
  list[minIndex] = list[startScan];
  list[startScan] = temp;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.