在优先级队列中出列队列

时间:2016-01-28 11:13:43

标签: c++ arrays sorting priority-queue

我已经使用数组(学校工作)实现了一个优先级队列,如下所示:

int dequeue(int a[], int n){

    int i, numberToDequeue;
    numberToDequeue = a[0];

    for(i = 0; i < n; i++){
        a[i] = a[i+1];
    }
    return numberToDequeue;
}

在优先级队列中出列应该花费O(1)时间。

但是,在我的代码中,出队和

需要O(1)时间

O(n)时间将所有元素向前移动1个索引。

我想知道是否有更好的解决方案,其时间复杂度为O(1)。

非常感谢所有形式的回复。

1 个答案:

答案 0 :(得分:0)

int dequeue(int a[], int n){ //provided that the numbers are in ascending order

    int numberToDequeue;
    numberToDequeue = a[n - 1];
    n--;
    return numberToDequeue;
}
相关问题