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 :)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With