使用另一个队列对队列进行排序

时间:2012-05-12 08:37:40

标签: sorting queue

如何使用另一个大小为N的队列和有限数量的变量对大小为N的队列进行排序?

天真的实现 - 找到队列的最小值并将其推送到空队列,然后找到新的最小值并推送它等等是O(n ^ 2)。有更高效的算法吗?

4 个答案:

答案 0 :(得分:0)

我认为天真的实施不会起作用。它是一个队列,所以如果它不在队列的末尾,你就不能删除最小的元素。

我能想到的唯一方法是: 始终保持第二个队列排序。 1.从q1中删除元素。 2.如果此元素> = q2的最后一个元素,则将其插入q2。 (那样q2仍然是排序的)。 3.否则,将其插回q1。 继续重复上述步骤,直到q1为空。

答案 1 :(得分:0)

我的算法:

 let the Q size = n, 
 1 save = getRearValue(1stQ)
 2 pop the element from 1st Q and insert it into 2nd Q.
 3 if getFrontValue(1stQ) > getRearValue(2ndQ)  
 4        then goto step 2.
 5 else 
 6        pop the element from 1st Q and insert back into the same Q (1st Q). 
 7        if (save != getRearValue(1stQ)) goto step 3.
 8 if (1st Q not sorted OR getFrontValue(1stQ) > getFrontValue(2ndQ))
 9          then 
 10          pop all elements of 2nd Q and insert into 1st Q (One by one).
 11          goto step 1.
 12 else
 13     pop all elements of 2nd Q and insert into 1st Q (One by one).
 14     return 1stQ

答案 2 :(得分:0)

这是一个简单的逻辑,我从头脑中想出来。最坏情况的运行时间将是O(N ^ 2),而理想情况下最好的情况可以是O(N)。我认为通过即兴逻辑可以进一步降低复杂性。

语法是Javascript,但很好地评论是不言自明的。

我希望这会有所帮助。

// SORT A QUEUE USING ANOTHER QUEUE
function sortQueueUsingQueue(uq) {
    // instantiate required variables
    var sq = new Queue();
    var t = null, last = null;
    // copy the items to a temp queue
    // so as not to destroy the original queue
    var tq = new Queue(uq);
    // loop until input is not empty
    while(!tq.isEmpty()) {
        t = tq.dequeue();
        if (last && last <= t) {
            // new element will be at the end of the queue, and we don't have to
            // process any further - short-circuit scenario
            // this is the best case scenario, constant time operation per new item
            sq.enqueue(t);
            // also keep track of the last item in the queue,
            // which in this case is the new item
            last = t;
        } else {
            // other scenario: linear time operation per new item
            // new element will be somewhere in the middle (or beginning) so,
            // take elements from the beginning which are less or equal to new item,
            // and put them at the back
            while(!sq.isEmpty() && sq.peek() <= t) {
                sq.enqueue(sq.dequeue());
            }
            // when that is done, now put the new element into the queue,
            // i.e the insertion into the proper place
            sq.enqueue(t);
            // following which, shift the rest elements to the back of the queue,
            // to reconstruct the sorted order,
            // thus always keeping the second queue sorted per insertion
            while(sq.peek() > t) {
                // meanwhile, also keep a track of the last (highest) item in the queue
                // so that we may perform a short-circuit if permitted
                last = sq.dequeue();
                sq.enqueue(last);
            }
        }

    }
    return sq;
}

您可以在此处将整个工作代码视为github gist https://gist.github.com/abhishekcghosh/049b50b22e92fefc5124

答案 3 :(得分:0)

这是我认为可行的方法: -

该算法使用Recursion对队列进行排序。

假设我们有一个函数copy_from,它可以从一个队列复制到另一个队列,如下所示: -

void copy_from (ArrayQueue&Q_from,ArrayQueue& Q_to){

     while(!Q_from.empty()){

        int t= Q_from.front();
        Q_to.enqueue(t);
        Q_from.dequeue();
     }

}   

主要排序功能如下所示: -

    void sort(ArrayQueue &Q, int element, ArrayQueue&Q2){

if(Q.size()==1){

    if(Q.front()>element){

         int front = Q.front();
         Q.dequeue();
         Q.enqueue(element);
         Q.enqueue(front);      
    }
    else{
        Q.enqueue(element);
    }

}
else{

 int front = Q.front();
 Q.dequeue();
 sort(Q,front); // sort the remaining queue.

 int sorted_front = Q.front(); //get front element of sorted queue
 if(element<sorted_front){

    Q2.enqueue(element);
    copy_from(Q,Q2);
    copy_from(Q2,Q);     

 }
 else{
    // dequeue all elements from the queue which are lesser than element and put it into new queue.
    // Then enqueue the new element
    // Then enqueue whatevers bigger than element into the queue.

      Q2.enqueue(sorted_front);
      Q.dequeue();

      while(!Q.empty()&&Q.front()<element){
         Q2.enqueue(Q.front());
         Q.dequeue();
      }

      Q2.enqueue(element);
      copy_from(Q,Q2);
      copy_from(Q2,Q);

 }

}
}

当我们最初调用sort函数时,我们可以按如下方式调用它: -

Queue Q, Q2;

 int front = Q.front();
 Q.dequeue();
 sort(Q,front,Q2);

如果我们输入6-> 4-> 9> 2-> 1,则结果将是9-> 6-> 4-> 2-> 1。