3,089 views

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

B)Heap

C)Sorted Array

D)Unsorted Array

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

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.

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.

3.Heap:

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. 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)

http://pages.cs.wisc.edu/~vernon/cs367/notes/11.PRIORITY-Q.html

https://en.wikipedia.org/wiki/Priority_queue#Implementation

@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.

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?

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
Answer is heap because heap takes minimum time in Enqueue and Dequeue as O(logn)
by

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

https://www.geeksforgeeks.org/priority-queue-set-1-introduction/
{"ft_lang_label":"__label__en","ft_lang_prob":0.7984303,"math_prob":0.81311816,"size":1040,"snap":"2022-40-2023-06","text_gpt3_token_len":226,"char_repetition_ratio":0.1949807,"word_repetition_ratio":0.058441557,"special_character_ratio":0.19807692,"punctuation_ratio":0.10215054,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9718711,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-05T10:33:21Z\",\"WARC-Record-ID\":\"<urn:uuid:8681d73f-1697-4733-9039-344a471ae262>\",\"Content-Length\":\"161675\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:898d0122-c313-4a5f-8a00-442ee432041b>\",\"WARC-Concurrent-To\":\"<urn:uuid:787ffe3a-1423-469a-83a4-c0f2012d9ce2>\",\"WARC-IP-Address\":\"104.21.19.123\",\"WARC-Target-URI\":\"https://gateoverflow.in/310669/made-easy-test-series-programming-and-ds?show=310709\",\"WARC-Payload-Digest\":\"sha1:BSG7WPWWUL3STLF6HXBA6FOBFSQRQJ5B\",\"WARC-Block-Digest\":\"sha1:WE3TIAFZZJH5JFXWVO6P7E5WUPWF34UF\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764500251.38_warc_CC-MAIN-20230205094841-20230205124841-00341.warc.gz\"}"}