Here are two questions that I got the wrong answer but I don't know why.
1.Suppose you implement the functionality of a priority queue using a sorted array (e.g., from biggest to smallest). What is the worst-case running time of Insert and Extract-Min, respectively? (Assume that you have a large enough array to accommodate the Insertions that you face.)
My answer is O(log(n)) and O(1) since we can binary search and insert so O(log(n))
extract-min is easy.
but the right answer is O(n) and O(1).
2.Suppose you implement the functionality of a priority queue using an unsorted array. What is the worst-case running time of Insert and Extract-Min, respectively? (Assume that you have a large enough array to accommodate the Insertions that you face.)
My answer is O(1) and O(log(n)) since the insert is arbitrary and extract-min we can first sort then take the minimum.
but the right answer is O(1) and O(n)
Could anyone help me with this problem?Thanks a lot.
Oh,I understand the 2nd question,sort takes O(n*log(n)) not O(log(n))