-1

I am trying to implement 'Insertion Sort' on two queues without using an array.

Queue 1 - 4, 5, 11, 8, 3

Queue 2 - 2, 3, 4, 5, 2, 11

After the sorting they are as following :

Queue 1 - 3, 4, 5, 8, 11

Queue 2 - 2, 2, 3, 4, 5, 11

They get sorted. But I sort the queue like if it was a list. I do not know how to deal with a FIFO structure.

My teacher said that my implementation is alright if it was for a list, but not for queue. I am supposed to use the push() and pop() functions (already implemented them) and a third queue for assistance. This is my current implementation of the sorting algorithm:

void InsertionSort(queue* &left, queue* &right)
{
    int x, i = 0, j;
    queue *p = left;
    while (p)
    {
        x = getElemAt(i, left, right);
        j = i - 1;
        while (j >= 0 && x < getElemAt(j, left, right))
        {
            setElemAt(j + 1, getElemAt(j, left, right), left, right);
            j--;
        }
        setElemAt(j + 1, x, left, right);
        p = p->next;
        i++;
   }
}

getElemAt and setElemAt are additional functions I've written separately. How should I approach the problem of sorting with an additional queue?

Community
  • 1
  • 1
  • What does "Insertion Sort' on two queues" mean? Insertion sort is usually performed on a single array. Can you give an example input and output, and also specify what the problem with your current code is? – interjay Dec 16 '15 at 18:10
  • Well my assignment says that I have 2 queues , that need to be sorted. The problem with my current code is that I access directly the second and the next elements ,which isn't possible in a Queue ,hence I need to use a third queue to assist with the sorting , to store previous /unsorted/sorted elements. I need to not violate the rule that a Queue is processed as first input ,first output (FIFO) – ClonnableInterface Dec 16 '15 at 18:35
  • I still don't understand what sorting two queues means. Does it mean to sort each one individually? That doesn't seem to be what the code above does. – interjay Dec 16 '15 at 18:43
  • I have 2 queues , left and right. They are being sorted with the code above. – ClonnableInterface Dec 16 '15 at 18:56
  • Yes, you have repeated that several times now, and it is still just as meaningless. How about an example of what the queues contain before and after sorting? – interjay Dec 16 '15 at 18:59
  • Queue 1 - 4 ,5 ,11 ,8,3 ,Queue 2 - 2,3,4,5 ,2 ,11. After the sorting they are as following : Queue 1 - 3 ,4 ,5 ,8,11 , Queue 2 - 2,2,3,4,5,11. They get sorted. But I sort the queue like if it was a list. I do not know how to deal with a FIFO structure and I am seeking advice here. – ClonnableInterface Dec 16 '15 at 19:05
  • Ah i see, in my solution I only sorted the first Q and that's pushed into second queue. – deebee Dec 16 '15 at 19:10
  • right . My solution works like this too. In the main method , it calls the InsetionSort function separately for the first queue and the second queue. – ClonnableInterface Dec 16 '15 at 19:13
  • Okay, deleted my answer, doesn't add any new thoughts. – deebee Dec 16 '15 at 19:15
  • Nah , your answer provided useful information , I think it would have worked in this case , you could post it again ,if it's not a problem. – ClonnableInterface Dec 16 '15 at 19:52
  • Added queues and results from the comments into the main text – dstudeba Dec 18 '15 at 18:51

1 Answers1

0

@interjay the queues need to be sorted individualy , with the use of a 3rd assisting queue which is used when queue1 or queue2 is sorted. The queues must not be sorted at the same time because this will require 2 assisting queues

rmonov
  • 16
  • Or if sorting two queues according to one of the queues, then the third queue can be used for pairs of elements. – rcgldr Dec 18 '15 at 17:43