You do find the smallest value however it is not the value that you return when you extract min:
//find smallest value
for (int i = front; i <= rear; i++){
if (array[i] < minValue)
minValue = array[i];
}
//return smallest value
return array[front]; //Not equal to the smallest value
I think that what you want to do is find the smallest number then remove it from the array and return it.
This is maybe not the most clean solution but it will solve your problem:
int minIndex = front;
//find smallest value
for (int i = front; i <= rear; i++){
if (array[i] < minValue)
{
minIndex = i;
minValue = array[i];
}
}
array[minIndex] = array[front];
//return smallest value
return minValue;
If i were to implement a priority queue i would ensure to allways put the smallest value at front and make sure that the array is sorted when i do insert.
int index = rear;
for(int i = front ; i <= rear ; i++)
{
if(x < array[i])
{
for(int j = rear ; j >= i ; j--)
{
array[j] = array[j-1];
}
index = i;
break;
}
}
array[index] = x;
Adding this to your insert would sort of work however first time when the following snippet is run front will be set to one. which means you will skip the first value in the queue.
else
{
front = (front+1) % size;
}
I would suggest making the above change in insert and change your extractmin to something like this:
//extract and return smallest value in queue
int priorityQueue::extractMin()
{
//Handle circulation.
//return smallest value
return array[front++];
}