edited by
51,725 views
143 143 votes

In a min-heap with $n$ elements with the smallest element at the root, the $7^{th}$ smallest element can be found in time

  1. $\Theta (n \log n)$
  2. $\Theta (n)$
  3. $\Theta(\log n)$
  4. $\Theta(1)$

11 Answers

Best answer
200 200 votes
Time to find the smallest element on a min-heap- one retrieve operation - $\Theta(1)$
Time to find the second smallest element on a min-heap- requires $2^2 - 1 = 3$ check operations to find the second smallest element out of $3$ elements - $\Theta(1)$

Time to find the $7^{th}$ smallest element - requires $O(2^7-1) = O(127)$ check operations to find the seventh smallest element out of $127$ possible ones - $\Theta(1)$

In short if the number of required operations is independent of the input size $n$, then it is always $\Theta(1)$.

(Here, we are doing a level order traversal of the heap and checking the elements)

If we are not allowed to traverse the heap and allowed only default heap-operations, we will be restricted with doing Extract-min $7$ times which would be $O(\log n)$.

Correct Answer: $D$
edited by
48 48 votes
The 7th most element is always within the 7 levels from the root so we have constant number of nodes in worst condition .we need to check only constant number of nodes to find the 7th smallest number so constant time
18 18 votes

$1st$ minimum will always at root of min heap. Hence, $0$ comparisons.

Now $2nd$ minimum can be child left child or right child of the root. Hence, $1$ comparison.

For the $3rd$ minimum we are assuming that $2nd$ min is the left child of root. So the $3rd$ minimum must be in the childs of $2nd$ min or right child of $1st$ min(root). Because if $3rd$ min is found other places than these nodes then min heap property will be violated (try yourself by some examples). So total comparison $2$.

Now same way I am assuming the $3rd$ min is the left child of the $2nd$ min so the $4th$ min can be found in the the area in the picture below. So to find $4th$ min we have to compare $4$ nodes means $3$ comparisons.

You can try the remaining yourself.

$1st \text{ min} \rightarrow \, 0 \text{ comparison}$

$2nd \text{ min} \rightarrow \, 1 \text{ comparison}$

$3rd \text{ min} \rightarrow \, 2 \text{ comparisons}$

$4th \text{ min} \rightarrow \, 3 \text{ comparisons}$

$5th \text{ min} \rightarrow \, 4 \text{ comparisons}$

$........................$

$........................$

$kth \text{ min} \rightarrow \, (k-1) \text{ comparisons}$

 

$\therefore$ To finnd $kth$ min we need $k(k-1)/2$ comp.

So to find $7th$ min we need  $7(7-1)/2 = 21$ comp.

So time needed $\Theta(1)$.

Option: D 

 

edited by
8 8 votes
The best known algorithm to find Kth smallest element in Min Heap takes - O(K*logK) time and here K = 7th element.

Thus we get O(7*log7) = O(1) as Answer.
3 3 votes
3 approaches....
1st- Selection Sort
7 passes to find 7th minimum i.e. 7 × n = O(n)

2nd- Deletion from heap
Deletion of 1 element is logn
Deletion of 7 elements is 7 ×logn = O(logn)

3rd- sum of 1st 6 natural number in heap
O(1)

Among all 3 approaches 3rd one is best so option d is the answer.
2 2 votes

Sine the 7th smallest element will be from  ciel( a[7/2] to a[7]   hence only “k” comparison are required thus 0(1) is the answer

Answer:
Position:
Show:

Related questions

61 61 votes
5 answers 5 answers
14.5k
14.5k views
Kathleen asked Sep 17, 2014
14,533 views
A program consists of two modules executed sequentially. Let $f_1(t)$ and $f_2(t)$ respectively denote the probability density functions of time taken to execute the two ...
80 80 votes
10 answers 10 answers
31.6k
31.6k views
Kathleen asked Sep 17, 2014
31,570 views
Consider the function $f$ defined below.struct item { int data; struct item * next; }; int f(struct item *p) { return ((p == NULL) || (p->next == NULL)|| ((p->data <= p -...
136 136 votes
17 answers 17 answers
48.5k
48.5k views
Kathleen asked Sep 17, 2014
48,543 views
Let S be a stack of size $n \geq1$. Starting with the empty stack, suppose we push the first n natural numbers in sequence, and then perform $n$ pop operations. Assume th...
94 94 votes
4 answers 4 answers
31.2k
31.2k views
Kathleen asked Sep 17, 2014
31,195 views
A data structure is required for storing a set of integers such that each of the following operations can be done in $O(\log n)$ time, where $n$ is the number of elements...