I'm having problems trying to implement a counting sort for a template linked list. I already coded everything and I get the correct output.
Now I just need to sort using counting sort method. When I run the project, the program crashes and it says:
First-chance exception at 0x00262805 in bleh.exe: 0xC0000005: Access violation writing location 0xcdcdcdf5.
Unhandled exception at 0x00262805 in bleh.exe: 0xC0000005: Access violation writing location 0xcdcdcdf5.
template <class DataType>
void LinkedList<DataType>::sort()
{
Node<DataType> **arr = new Node<DataType> *[numElements];
int i;
int j;
Node<DataType> *ptr = start;
Node<DataType> *tempPtr;
i=0;
while(ptr->next != NULL)
{
arr[i] = ptr;
ptr = ptr->next;
i++;
}
for(j = 1 ; j < numElements-1; j++)
{
tempPtr = arr[j];
i = j - 1;
while(i > -1 && (arr[i]->info.price * 100) > (tempPtr->info.price * 100))
arr[i + 1] = arr[i];
i--;
arr[i+1] = tempPtr;
}
/*for(i = 0; i < numElements - 2; i++)
{
arr[i]->next = arr[i + 1];
}
start = arr[0];
arr[numElements - 1]->next = NULL;*/
}
Teacher gave us the pseudo code and we're supposed to implement it, so I wonder if I'm having logic problems.
By the way the last piece of code that is commented out, I believe is what is wrong since if I run the code I don't get no errors but the output is not sorted (of course). If I use the last piece of code is when the program crashes.
Any help would be appreciated.