MCQ IN COMPUTER SCIENCE & ENGINEERING

COMPUTER SCIENCE AND ENGINEERING

ALGORITHMS

Question [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
What is the time complexity of this codeint a = 0, i = N;while (i > 0) { a += i; i /= 2;}
A
O(N)
B
O(Sqrt(N))
C
O(N / 2)
D
O(log N)
Explanation: 

Detailed explanation-1: -Linear Time Complexity O(n) occurs when the run time of the code increases at an order of magnitude proportional to n. Here n is the size of the input. Logarithmic Time Complexity O(log n) occurs when at each subsequent step in the algorithm, the time is decreased at a magnitude inversely proportional to N.

Detailed explanation-2: -n indicates the input size, while O is the worst-case scenario growth rate function. We use the Big-O notation to classify algorithms based on their running time or space (memory used) as the input grows. The O function is the growth rate in function of the input size n .

Detailed explanation-3: -for(var i = 0; i < length; i++) //has O(n) time complexity for(var j = 0; j < length; j++) //has O(n^2) time complexity // More loops? Other examples of quadratic time complexity include bubble sort, selection sort, and insertion sort.

There is 1 question to complete.