10

I have been asked this question and I think it's doable, However I am having a hard time coming up with an algorithm to do this. The restrictions is that you can not use any other data structure nor create another queue. Also you only can use enqueue, dequeue and peek (NOT a priority queue).

Thanx for contributing :)

3ashmawy
  • 469
  • 1
  • 7
  • 16
  • I'd be very surprised if you could do this. However, I think you need to be a little more specific about what kind of temporary memory you're allowed to use. I'm assuming you're only allowed `O(1)` memory? – ltjax Feb 27 '11 at 15:58
  • There are no limits neither on time nor space complexities. However It's kind of implied that space complexity would be 1, Unless there is a recursive solution and counting StackFrames created per recursive call is considered adding to space complexity. – 3ashmawy Feb 27 '11 at 16:03
  • Does `peek` only return the head of the queue, or can you use it to find the value of any position the queue? – IVlad Feb 27 '11 at 16:05
  • Only the head of the queue :) – 3ashmawy Feb 27 '11 at 16:05
  • @3ashmawy: Stack frames created have to be counted, otherwise there is a very simple solution. – ltjax Feb 27 '11 at 16:14
  • @Itjax Can you please elaborate more on the solution you have in mind, There is absolutely no limits on complexities – 3ashmawy Feb 27 '11 at 16:17

2 Answers2

13
Sort(Q,n):

  for i = 0 to n-1
    min = findMin(Q, n-i, n)
    reorder(Q, min, n)
  end for

findMin(Q, k, n):

  min = infinity

  for i = 1 to n
    curr = dequeue(Q)
    if curr < min && i<=k
      min = curr
    end if
    enqueue(Q,curr)
  end for

  return min

reorder(Q, min, n):

  for i = 1 to n
    curr = dequeue(Q)
    if (curr != min) // I'm assuming that there is a way to differentiate between any two items, this isn't hard to enforce
      enqueue(Q, curr)
    end if
  end for

  enqueue(Q,min)

Ascending sort, runs in O(n^2) time, in space O(1) (i.e. the queue is O(n) space, and extra space required by the algorithm is O(1))

Explanation:

Essentially, we iterate through the queue n times, each time the iteration costs 2n.

On each iteration, we go through the entire queue and pick the minimum within the relevant number of items. So at first the relevant number of items is n, since we want the minimum of them all. The next iteration we want the minimum of the first n-1 items, and then n-2 items, etc. Since the algorithm will stack these minimums at the end.

Once we found the minimum, we need to iterate over the whole stack again in order to snatch it and stack it on the end.

The main idea here, is that a dequeue followed by an enqueue of that same element will allow us to iterate over the queue and check minimum/maximum while preserving order.

davin
  • 44,863
  • 9
  • 78
  • 78
  • damn, i came up with the same solution and as i came back to post it, i saw your answer already posted...eeeeeeh..speed ;)...i upvoted you for the speed – Suraj Chandran Feb 27 '11 at 16:47
  • @Suraj, if i got 1 rep. point for every time that happened to me, SO would have to invent a new data type to hold BigBigBigInt :) – davin Feb 27 '11 at 16:59
4

BubbleSort using a queue:

n <- size of queue
repeat n times
  x <- dequeue item
  repeat (n-1) times
    y <- dequeue item
    if x < y then
      enqueue y
    else
      enqueue x
      x <- y
    end
  end
  enqueue x
end
Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138
  • Thank you for the awesome answer it helped me a lot. Just one thing i noticed here is that there has been a typo in the above pseudo code. The line enqueue x (the last line) is having an extra space in the beginning. So it is appearing as though enqueue x should be repeated (n-1) times instead of n times. – akshayKhanapuri Jun 28 '20 at 11:05
  • 1
    @akshayKhanapuri I added `end` markers, to make the boundaries clearer. – Markus Jarderot Jun 29 '20 at 09:32