Log In
2 votes

Which of the following data structure is efficient to implement priority queue such as insertion ,deletion, searching?

A)Linked List


C)Sorted Array

D)Unsorted Array

How priority queue can work more efficiently in any data structure, other than heap?

in DS 466 views
i think Heap is generally preferred for priority queue implementation . it gives better performance other thamn linked list and array.

IN Array:

1.  ARRAY: 

  i) insert operation can be implemented by adding an item at end of array in O(1) time .

 ii) getHighestPriority  operation can be implemented by linearly searching the highest priority item in array. This operation takes O(n) time.

iii) deleteHighestPriority  operation can be implemented by first linearly searching an item, then removing the item by moving all subsequent items one position back.

2.Linked List:

 time complexity of all operations with linked list remains same as array. The advantage with linked list is deleteHighestPriority can be more efficient as we don’t have to move items.


  i)In a Binary Heap, getHighestPriority  can be implemented in O(1) time, insert() can be implemented in O(Logn) time and deleteHighestPriority  can also be implemented in O(Logn) time.
ii) fibanooci heap  insert() and getHighestPriority can be implemented in O(1) amortized time and deleteHighestPriority can be implemented in O(Logn) amortized time.

Hence Heap is generally preferred for priority queue implementation .


in wiki also told as heap, but ans is sorted array

no, ans is something else
maa'm  how sorted array is corret answer?/
I think it is because in sorted array, we can do insertion , deletion, searching in $O\left (  logn \right )$ time.

but heap need build heap method which is $O(n)$ time
In sorted array for deletion and insertion, we have to perform shifting which takes O(n) time

@Anuj Mishra

I yesterday saw somewhere, deletion will take O(1) time. As because, there is certain priority on elements of queue

It will be called Extract-Min then which will extract the top element. That way we can do in O(1) time. I guess you're right we delete only max element but what about insertion, that can't be done in O(1) it will take O(n) for shifting also array will need to have dynamic size.

4 Answers

1 vote

Extract from Narsimha Karumanchi which must have made things clear, Someone on Stack Overflow said "Get an algorithms textbook, it is better than random SO questions"

Refer this link @srestha Ma'am , Also sorted array isn't better than heap because logn is much much smaller than n, and also because there are equally large number of insertions, Delete Max and Finds (And 2logn is still way smaller than n)


@srestha ma'am If you first refer Narsimha book, you'll get better explanations and also the right concept, @Gateasp2020 refer textbooks instead of online links (I guess you are not doing so and trust me books are much better ), @yajush mishra first understand the context of the question then answer.

Hope I don't offend anyone, just giving honest advice not criticizing anyone.

edited by

@Anuj Mishra

u just copy-paste the answer. Can u explain the concept a bit?


have you read my above comment on the question?

Sorry for not addressing your main question:

How priority queue can work more efficiently in any data structure, other than heap?

@srestha maam please read this: 

First off, How can you deduce this? It is wrong to say anything without workload or number and type of operations we're going to perform, we can only say things, in general case (In priority queues we need to find and extract min or top priority element and since it's a queue there will be a decent number of insertions and deletions so we need an implementation which performs decently on these three operations) 

Now which data structure to choose? 

Sorted Array:

As I earlier mentioned if we insert an element in somewhere middle we need to shift remaining elements so takes O(n) time, deletion and search are always for top priority element so O(1).  

In case of heaps you know we can find top priority element in O(1) as it's root element, for deleting and inserting elements we need to call heapify which takes O(logn) time.

Unsorted Array: Deletion requires O(n) time as element can be anywhere in middle thus require shifting, insertion can be done anywhere, linear search for find.


@Anuj Mishra

that means in deletion elements gets priority, which will deleted first

But in case of insertion, there is no priority where to be inserted 

Why So?

In priority queue , insertion is like normal array?

Normal Deletion (any random element whose value is given) will take O(logn) time but if you are doing normal Deletion and normal search then why do you need "Priority Queue", It can be seen with example of ready queue of processes, we need to extract shortest job (highest priority) everytime, but new incoming jobs can have  any burst time, so insertion is done without seeing which elements are already there or considering priority stuff, but when we extract we need highest priority process. Thus operations of priority queue are find max, extract max or Delete max .

@Anuj Mishra

yes, but our main concern is to know how priority queue work more efficiently in sorted array than heap

In case of insertion deletion searching

Sorted array takes-$O\left ( n \right )+O(1)+O(logn)$

In heap it takes-$O( logn)+O(logn)+O(n)$

So, checking total complexity, we can say sorted arrayis more efficient



@srestha maam, what are Priority queue operations?

1)Insert, Delete and Find ?


2)Insert, find-min,delete-min ?

Also, check out the links that I've added in answer.


@Anuj Mishra

why we need to find min element only, we can find anything and also it maynot have any priority too

Then why find-min operation?

It will be just a find operation 

is it not?

In SO they would have meant a standard book like Cormen 😄
which line of cormen?

@Arjun sir you're right cormen is better, but It doesn't have such comparison between different implementations of priority queue as is done in Narsimha book,

@srestha ma'am, 3rd edition chapter 6 -Chapter notes(For future reference just do ctrl+F in PDF version)


@Anuj Mishra Narsimha book has a lot of errors. It is useful to get placement questions -- not for learning concepts. If you say you follow this book for algorithms in any IIT interviews there is a high chance you won't be selected. But many IIT people follow this for placements.

Thank you Sir, I'll keep this in Mind, I didn't knew about errors, I've used it for practicing problems during gate preparation , luckily I'm following cormen because I wanted to see proof of correctness for algorithms, Now I'll see cormen exercises too

@Anuj Mishra

I checked cormen

But they mention priority queue as heap structure. Nothing mentioned about sorted array.


Narashima book mentioned time complexity

But not mentioned which one better in performance.

@Arjun Sir

Can u point out, why sorted array is better in performance than heap , in case of priority queue?

For this question you do not need any book. The given answer is the best for the asked question. And your doubts are also genuine for the asked question. I'll answer this once I finish answering better questions here.
ok :)
is that mean heap can be answer too?
Where is it mentioned that sorted array is better to implement priority queue, everywhere I read it is mentioned that Heaps are used. Can you provide justification to how you come to this conclusion that sorted array is better here?
The question asks for "Priority Queue" - assume minimum value is maximum priority. Now, if we use a min-heap we get $O(\log n)$ insertion and deletion. Here, deletion is mostly done for maximum priority (extract-min) and rarely for minimum priority or other values (can be even ignored as they do not consititute necessary priority queue functions). Find is also for maximum priority and can be done in $O(1).$

Now, if we use a sorted array, find will be $O(1)$ -- we search for maximum priority which should be the first value.

Insert will be $O(n)$ because we might need to shift all $n$ elements even if we can find the position in $O(\log n)$ time like in Insertion Sort.

Deletion will be $O(n)$ as we need to shift $n-1$ elements one place.
Min. heap clearly wins rt?
0 votes

                                   linked list           heap               sorted array             unsorted array

insertion                        O(N)                O(log N)              O(N)                         O(1)

deletion                         O(N)                O(log N)               O(N)                        O(N)

searching                      O(N)                  O(N)                  O(log N)                     O(N)


judging by this, I have two main contenders heap and unsorted array. If more insertions than deletion then i will pick unsorted array but in general heap should be the answer so option B

no, I donot think it is correct

because sorted array insertion deletion cannot be $O(n)$
and also tell me how priority queue operating on sorted array
0 votes
Answer is heap because heap takes minimum time in Enqueue and Dequeue as O(logn)
0 votes

Heap is generally preferred for priority queue implementation because heaps provide better performance compared arrays or linked list. In a Binary Heap, getHighestPriority() can be implemented in O(1) time, insert() can be implemented in O(Logn) time and deleteHighestPriority() can also be implemented in O(Logn) time.

B is correct option 

for good read

Related questions

0 votes
2 answers
4 votes
3 answers
#include<stdio.h> #include<iostream> int bar(int m, int n){ if(m==0)return n; if(n==0)return m; return bar(n%m,m); } int foo(int m,int n){ return(m*n/bar(m,n)); } int main(){ int x=foo(1000,1500); printf("%d",x); return 0; } Output of the program is ___________
asked May 20, 2019 in Programming srestha 402 views
2 votes
1 answer
Consider the following $C$ implementation which when given $3$ numbers a,b,c as input, find the maximum of $3$ numbers $a,b,c.$ int kickstart(int a,int b,int c) { if(B1) return a; if(a>=b) return B2; return kickstart(c,a,b); } How the boxes filled up correctly? $I)B1:a\geq b$ ... $IV)B1:a\geq b$ && $a\geq c, B2:kickstart\left ( b,c,a \right );$ Is it $I) and II)$ or $I) and IV)$
asked May 19, 2019 in Programming srestha 435 views
2 votes
2 answers
Consider the following function $foo()$ void foo(int n){ if(n<=0) printf("Bye"); else{ printf("Hi"); foo(n-3); printf("Hi"); foo(n-1); } } Let $P(n)$ represent recurrence relation, indicating number of time print statement executed. What will best recurrence for ... $n=0$ The options are confusing to me. Can someone explain the options well. Moreover , what will be constant added $1$ or $2?$
asked May 12, 2019 in Programming srestha 343 views