“All O(1) functions take exactly the same amount of time to run.” True or false? Can anybody explain the answer to me?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
False. O(1) means constant time. This means that no matter what the size of the input is, the function will run in more or less the same amount of time – the runtime does not scale with the input.
This means that two O(1) functions will each run in constant time, though their constants may be different. So if you have two O(1) functions
fandg, each of which compute the same result, expecting similar inputs (lets say they expect lists, for the sake of discussion), the runtime offdoes not depend on the size of the list; nor does the runtime ofg.However, if
fmakes more complicated (or otherwise time-consuming) steps to compute the answer than doesg, thenf‘s runtime will be more thang‘s – the number of seconds required forfto terminate (let’s call this valuefsec) will be more than the number of seconds required forgto terminate (let’s call this valuegsec). Still, neitherfsecnorgsecdepend on the size of the input list – they’ll be the same no matter how big or small the input list – butgsecwill always be smaller thanfsec.It is because the runtime does not depend on the size of the input list that they are classified as O(1) algorithms – NOT because they perform a particular number of operations.