DEV Community

Discussion on: Brief Performance Analysis of Arrays and Objects through the lens of Big O Notation.

Collapse
 
adafia profile image
Samuel Adafia

Thank you, Charles. In response to your question, yes you can achieve a big O of O(log(n)) when searching through an array. However, two things will have to happen before a search operation on an array can achieve a big O of O(log(n)). The first condition is that the array has to be sorted. The second is, the method of search has to be binary instead of linear. This is because the number of things to search through is halved in every iteration until the element you are looking for is found.
Unsorted arrays on the other hand can only be searched using a linear search method hence will maintain a runtime of O(n) aka linear time.
This is a great question. I'll update my post to include this information. Although judging from the way it's coined I'm tempted to believe you already know the answer. Once again thank you so much for your question.

Collapse
 
chaluwa profile image
Odili Charles Opute

Great response and you are correct about me already knowing ... I just wanted to prompt you, in case you need to update your already nice article.