MCQ IN COMPUTER SCIENCE & ENGINEERING

COMPUTER SCIENCE AND ENGINEERING

DATA STRUCTURES

Question [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Time complexity of linear search:
A
O(n)
B
O(logn)
C
O(1)
D
O(n2)
Explanation: 

Detailed explanation-1: -Time complexity for linear search is denoted by O(n) as every element in the array is compared only once. In linear search, best-case complexity is O(1) where the element is found at the first index. Worst-case complexity is O(n) where the element is found at the last index or element is not present in the array.

Detailed explanation-2: -When we analyse an algorithm, we use a notation to represent its time complexity and that notation is Big O notation. For Example: time complexity for Linear search can be represented as O(n) and O(log n) for Binary search (where, n and log(n) are the number of operations).

Detailed explanation-3: -Time Complexity of Linear Search The best-case complexity is O(1) if the element is found in the first iteration of the loop. The worst-case time complexity is O(n), if the search element is found at the end of the array, provided the size of the array is n.

Detailed explanation-4: -Linear search makes n/2 comparisons on an average where n is the number of elements. At the most, linear search takes n comparisons. So, overall complexity in the worst case of linear search algorithm is O(n).

Detailed explanation-5: -The maximum number of iterations = (Number of times n is divided by 2 so that result is 1) = logN comparisons. t can vary from 0 to logn. The dominant term is n* logn / (n+1), which is approximately logn. Thus, we can conclude that the average case Time Complexity of Binary Search is O(logN).

There is 1 question to complete.