Hello. I am learning C++, and I am a little stuck. Right now, I'm working on pointers. The problem I'm working on requires sorting an array of structures, using pointers instead of array indices, using a selection sort. My code is not working, and I can't quite figure out why. I am able to do the problem using a bubble sort, however.
I am pretty new to pointers and sorting algorithms in general, so it's possible I am just missing something really simple. Here is the code:
void selection_sort(Student *ptr, int size)
{
int start,
min_index,
min_value;
Student temp;
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[start].score < min_value) {
min_value = ptr[index].score;
min_index = index;
}
}
// the following lines are where I think the problem lies,
// but I can't figure out the solution
temp = *(ptr+min_index);
*(ptr+min_index) = *(ptr+start);
*(ptr+start) = temp;
}
}
Right now, the output of this code after being passed to main is just an unsorted list, with the structures in the same order as when they were passed. It's not a scope issue, as I tried outputting the results within this function and got the same result.
I am still trying to wrap my head around the proper implementation of pointers (and, honestly, the selection sort as well), so any help would be greatly appreciated. Thank you.