Open In App

Data Structures and Algorithms | Set 38

Last Updated : 21 Feb, 2019
Like Article

This topic contains basic questions of Algorithm which can be helpful for GATE CS Preparation. So, it is recommended to solve each of these questions if you are preparing for GATE.

Ques-1: Which one of the following correctly determines the solution of the recurrence relation given below with T(1) = 1 ?

T(n)= 2T(n/4) + n1/2 

(A) O(n2)
(B) O(n)
(C) O(n1/2 log n)
(D) O(log n)

According Master Theorem,

T(n)= 2T(n/4) + n1/2 

Applying Masters Theorem,

a = 2, b = 4, K = 1/2, and p = 0 

So, bK = 41/2 = 2 
Thus, a = bK and (p > -1) 

So, the formula is,

T(n)= O(nlogba log(P+1)n)


T(n) = O(nlog 42 log(0 + 1)n) = O(n1/2 log n)

So, option (C) is correct.

Ques-2: For merging two unsorted list of size p and q into sorted list of size (p + q). The time complexity in terms of number of comparisons is:

(A) O(log p + log q)
(B) O(p log p) + q log q)
(C) O(p + q)
(D) None

For sorting the array of size p individually it takes O(p log p) and the array of size q takes O(q log q) time, then merging will take O(m + n) time.
Therefore, total number of comparisons

= O(p log p) + O(q log q) + p + q
= O(p log p) + O(q log q) 

So, option (B) is correct.

Ques-3: Which of the following sorting algorithms has the highest best case time complexity using array data structure ?

(A) Heap sort
(B) Insertion sort
(C) Bubble sort
(D) Selection sort

Best case time complexity of Heap sort is O(n log n)
Best case time complexity of Insertion sort is O(n)
Best case time complexity of Bubble sort is O(n)
Best case time complexity of selection sort is O(n2).
So, option (D) is correct.

Ques-4: Which of the following input gives the best case time for selection sort ?

(A) 1 2 3 4 5 6 7 8 9
(B) 2 3 1 5 9 7 8 6
(C) 9 8 7 6 5 4 3 2 1
(D) All of the above take same amount of time.

Selection sort in worst case and best case takes same time.
So, option (D) is correct.

Ques-5: What is the time complexity of recursive function given below:

T(n)= 4T(n/2) + n2 

(A) O(n2)
(B) O(n)
(C) O(n2 log n)
(D) O(n log n)

According Master Theorem,

a = 4, b = 2, k = 2, p = 0 

So, bk = 4 i.e., a = bk 

Therefore, the formula is

T(n) = O(nlog ba log(P+1)n)

So, T(n)= O(nlog 24 log(0 + 1)n) = O(n2 log n)

So, option (C) is correct.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads