edited by
5,375 views
19 votes
19 votes
Match the pairs in the following:$$\begin{array}{ll|ll}\hline \text{(A)} & \text{$O (\log n)$} & \text{(p)} & \text{Heapsort} \\\hline  \text{(B)} & \text{$O (n)$} & \text{(q)}& \text{Depth-first search} \\\hline   \text{(C)}& \text{$O (n \log n)$} & \text{(r)}  & \text{Binary search} \\\hline  \text{(D)} & \text{$O (n^2)$} &\text{(s)}  & \text{Selection of the $k^{th}$ smallest element in a set of $n$ elements}  \\\hline \end{array}$$
edited by

3 Answers

Best answer
24 votes
24 votes
$$\begin{array}{|ll|ll|}\hline \text{(A)} & \text{$O (\log n)$} & \text{(r)} & \text{Binary search} \\\hline  \text{(B)} & \text{$O (n)$} & \text{(s)}& \text{Selection of the $k^{th}$ smallest element in a}\\& && \text{set of $n$ elements (Worst case)} \\\hline   \text{(C)}& \text{$O (n \log n)$} & \text{(p)}  & \text{Heap sort} \\\hline  \text{(D)} & \text{$O (n^2)$} &\text{(q)}  & \text{Depth-first search }\\&&&\text{(It will be $O(n+m)$ if the graph is given in}\\&&&\text{the form of adjacency list. But if the graph} \\&&&\text{is given the form of adjacency matrix then the}\\&&&\text{complexity is $O(n \times n)$, as we have to traverse}\\&&&\text{through the whole row until we find an edge)}  \\\hline \end{array}$$PS: $k^{th}$ smallest element can be found in $O(n)$ time using partition algorithm.

$T(n)=T(n/2)+n$ (For finding $k^{th}$ smallest element) if the graph is given in the form of adjacency list and by Masters Theorem it will be done in $O(n)$.
edited by
7 votes
7 votes
(A) O(logn) (p) Binary search since applied on sorted so take logn time only evry time half number visit.
(B) O(n) (q)  Selection of the kth smallest element in a set of a elements [ using partition algorithm ]
(C) O(nlogn) (r)   Heapsort [O(n) + (nlogn)
(D) O(n2) (s) Depth-first search [using Adjacency Matrix in worst case visit each entry ]
edited by
1 votes
1 votes

 

(A) O(logn)     Binary search

 

(C) O(nlogn)    heap sort 

I think this two are straight forward.

 

(B) O(n) for BFS and DFS it takes O(E+V) time, here we may assume that n is number of vertices or number of edges depending on which one is greater 

D) O(n2)  please refer case 4 on  https://www.geeksforgeeks.org/kth-smallestlargest-element-unsorted-array/ and 

here we are using modified version of quick sort,

for example if we want to find 3 largest element i.e. k=3 , then at some point while execution of quick sort , it will pick some element as pivot for current loop and will put it at third position. 

Worst case complexity is O(n^2) . average case is O(n) bu we always go for worst case hence   O(n^2)                    .                  

Related questions

28 votes
28 votes
5 answers
1
makhdoom ghaya asked Nov 27, 2016
12,896 views
Match the pairs in the following:$$\begin{array}{ll|ll}\hline \text{(A)} & \text{Virtual memory} & \text{(p)} & \text{ Temporal Locality} \\\hline \text{(B)} & \text{Sha...
29 votes
29 votes
3 answers
2
makhdoom ghaya asked Nov 27, 2016
5,904 views
Match the pairs in the following questions:$$\begin{array}{ll|ll}\hline \text{(A)} & \text{Base addressing} & \text{(p)} & \text{Reentranecy} \\\hline \text{(B)} & \text...
10 votes
10 votes
3 answers
3
makhdoom ghaya asked Nov 29, 2016
1,621 views
Show that {NOR} is a functionally complete set of Boolean operations.
26 votes
26 votes
2 answers
4