Sorting a queue using same queue

前端 未结 2 802
迷失自我
迷失自我 2021-02-20 10:09

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 ot

2条回答
  •  爱一瞬间的悲伤
    2021-02-20 10:57

    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
    

提交回复
热议问题