Which of the following algorithms will run in constant time?

Explanation:

An algorithm runs in constant time when the time it takes does not depend on the size of the input. This is written as O(1) in time complexity. Accessing an element in an array using its index is a perfect example because the computer can directly go to that memory location in one step. It does not matter whether the array has 10 elements or 10,000 elements. In contrast, finding the largest number, sorting an array, or performing a linear search usually takes more time as the input grows. That is why indexed array access is considered constant time.

 
 

Latest Blogs