I am trying to write a report where I evaluate the time complexity of an algorithm I have designed, I know for sure that it's complexity is O(n). From what I got from Wikipedia, the best case would be O(1), if I have understood correctly it means that the best case is when the ArrayList I am using only contains one element, but I don't get the worst case completely, what does "O(1) iterative" mean and how can it occur?
-
1Best case complexity for Linear Search is O(1): Which means that the value you are looking for is found at the very first index. Worst Case time complexity is O(n) which means that value was not found in the array (or found at the very last index) which means that we had to iterate n times to reach to that conclusion. – Mushif Ali Nawaz Dec 11 '18 at 03:07
-
*Average* complexity is that you (on average) have to search half the list for the item, so O(n/2), but since constants are eliminated, it is O(n), i.e. the same as worst case. – Andreas Dec 11 '18 at 03:11
-
You can achieve O(1) time complexity by using hash based algorithm, which obviously needs more space to store hash values... :)... read about this when you have time. – Ketan Dec 11 '18 at 03:23
2 Answers
The reason it's O(1) best case is not JUST for a list with 1 element (although this would be the case in that scenario too). Imagine you have a list of 10 numbers.
[44,6,1,2,6,10,93,187,33,55]
Let's say we run Linear Search and are searching for the integer 44. Since it's the first element in the list, our time complexity is O(1), the best case scenario, because we only have to search 1 element out of the entire list before we find what we're looking for.
Let's look at a varient of that list.
[55,6,1,2,6,10,93,187,33,44]
In this case we swapped the first and last numbers. So when we run Linear Search for the integer 44 it will be a time complexity of O(n), the worst case, since we have to traverse the entire list of n elements before we find our desired element (if it even exists in the list, in our case is does).
Regarding the "O(1) iterative" on Wikipedia, I wouldn't let it confuse you. Also notice that it's referring to space complexity on the Wikipedia page, and not time complexity performance. We don't need any extra space to store anything during Linear Search, we just compare our desired value (such as 44 in the example) with the elements in the array one by one, so we have a space complexity of O(1).
EDIT: Based upon your comment:
In my case I am not looking for an element of the list in particular
Keep in mind "Linear Search" is a particular algorithm with a specific purpose of finding a particular element in a list, which you mention is NOT what you're trying to do. It doesn't seem Linear Search is what you're looking for. Linear Search is given an array/list and a desired element. It will return the index of where the desired element occurs in the list, assuming it does exist in the list.
I would always need to go thought the whole list fro mthe first to the last element
From your comment description, I believe you're just trying to traverse a list from beginning to end, always. This would be O(N) always, since you are always traversing the entire list. Consider this simple Python example:
L1 = [1,2,3,4,5,6,7,8,9,10] #Size n, where n = 10
for item in L1:
print(item)
This will just print every item in the list. Our list is of size n. So the time complexity of the list traversal is O(n). This only applies if you want to traverse the entire list every time.

- 1,404
- 5
- 22
- 49
-
Thank you for your fast reply, In my case I am not looking for an element of the list in particular, but I need to check if every single element's attribute is true or false (It's a list of objects), I would always need to go thought the whole list fro mthe first to the last element, so what would be the worst and best case? – jaymartines Dec 11 '18 at 03:27
-
1@jaymartines - In that your case, your description of your problem as "linear search" is incorrect. – Stephen C Dec 11 '18 at 03:31
-
@jaymartines It seems as if Linear Search might not be what you're looking for to accomplish the task. Linear Search is supposed to be given an element (we'll say X) and a list/array (we'll say L1). It starts from the beginning of L1 and traverses the list one by one. If it finds element X in the list L1, it returns the result, usually the index, of element X. What you're trying to do is essentially just a "for each" loop. You want to traverse every element in the list EVERY time. So no matter what, you traverse n elements in a list of n elements. So your best & worst case is O(n). – Birdman Dec 11 '18 at 03:33
-
@jaymartines I think I figured out what you're trying to accomplish. You mentioned in your post you're analyzing an algorithm YOU designed. Obviously you did not design linear search. I think you have a larger algorithm and you're just trying to identify what the time complexity is for a portion of it, correct? And that portion is where you want to traverse every element in a list, correct? (what you've been thinking of as a 'linear search', when in fact linear search is a whole separate algorithm with a specific purpose) – Birdman Dec 11 '18 at 04:17
-
*""Since it's the first element in the list, our time complexity is O(1)* ... No, I don't think this is accurate. The time complexity of an algorithm is a characteristic of the algorithm's overall performance. This term cannot be applied to a particular special case for an algorithm. – scottb Dec 11 '18 at 04:30
-
@scottb How would you reword it then? I think the overall point is clear, but maybe word choice was not optimal. – Birdman Dec 11 '18 at 06:24
-
When performance pertains to the inherent behavior of the algorithm (and not a special case of the data on which it operates), we can talk about its time complexity, eg. "when the hashcode() function returns the same value for all keys, the performance degrades to quadratic in n (O(n^2))". Your case gives a specific example of a set of data which is resolved on the 1st iteration. Your algorithm is still O(n) (even though your example has it resolving on the 1st item) unless you mean to imply that the algorithm resolves on the 1st iteration for all data sets on which it would ever operate. – scottb Dec 11 '18 at 21:18
In a comment you write:
In my case I am not looking for an element of the list in particular, but I need to check if every single element's attribute is true or false.
This is not a linear search. Searching (linear or otherwise) is answering the question "is there at least one matching element". Your question is "do all elements match".
I would always need to go thought the whole list fro mthe first to the last element, so what would be the worst and best case.
The best case is still O(1). If you find that one of the element's attribute is false
, you can terminate the scan immediately. The best case is when that happens for the first element ....
Consider this. Checking that "all elements are true" is equivalent to check that "NOT (some element is false)".

- 698,415
- 94
- 811
- 1,216
-
I'm not sure the second part is correct, as he doesn't in detail go into his problem. He just mentions he needs to check whether every element is true or false. He doesn't mention that every element should be true, and that upon reaching a false element the loop should terminate. Maybe he just wants to have 2 counters which add up the number of true elements in the list and the number of false elements in the list, therefore needs to traverse the entire list. I don't think he mentioned he needs to terminate for any reason before reaching the end. – Birdman Dec 11 '18 at 03:41
-
Well ... maybe. (I am reading the comment differently. Note that he describes this as a search not a count operation.) But suppose that element attribute can be true or false or *something else*. Now, I just need to scan for the "something else" value. Which is still O(1) in the best case! – Stephen C Dec 11 '18 at 03:45
-
1*"I don't think he mentioned he needs to terminate for any reason before reaching the end."*. I know he doesn't. But that is the optimization that can be done for "for all" and "exists" operations. And it is the optimization that he is alluding to in the Wikipedia article. – Stephen C Dec 11 '18 at 03:53
-
So if you want to know what I am doing in particular, I have an arrayList of Objects, every Object is a person, with attributes name, surname, age etc..; I want to go through the List of Objects, for (Person data : people), if (data.name.equals("mario") { Sysout.println(data.name)} – jaymartines Dec 11 '18 at 04:56
-
So I am not looking for the first "mario", but I want Every "mario" Person, The longer is the list, the longer is the running time, But I can't identify big O notation for best and worst case – jaymartines Dec 11 '18 at 04:58
-
@jaymartines - I think you should have put all of this information into your Question! The answer is that is *should* be `O(N)` in all cases. – Stephen C Dec 11 '18 at 05:30