Example of when I would choose O(n) algorithm over O(1) algorithm if for all of n, O(1) is faster than O(n)
Asked
Active
Viewed 3,229 times
0
-
Yes, that's exactly when. – jonrsharpe Oct 27 '16 at 20:34
2 Answers
0
One example is the O(1) algorithm consumes lots of memory while the O(n) one does not. And memory is more important for you compare to performance.

Arashsoft
- 2,749
- 5
- 35
- 58
-
2(_Please_ be careful with the case in notation for asymptotic behaviour of functions - _small o_ means _argument is strictly larger_ - algorithms in o(1) would need to finish in strictly less than constant time.) A non-parallel O(1) algorithm can touch O(1) memory, only. Your reasoning would be valid for O(n²) over O(n). – greybeard Oct 27 '16 at 21:18
-
@greybeard, I understand your point but I think this answer can be valid. Lets assume both algorithms uses O(1) memory. But, o(n) uses 1 byte and the o(1) algorithm uses 1000 byte. – Arashsoft Oct 28 '16 at 14:21
-
What @greybeard was complaining about was the casual way you're using [little-o](https://en.wikipedia.org/wiki/Big_O_notation#Related_asymptotic_notations) instead of [big-O](https://en.wikipedia.org/wiki/Big_O_notation). They are *not* the same thing! – Mark Ransom Oct 28 '16 at 14:28
-
0
Often, real data lends itself to algorithms with worse time complexities. For example, bubble sort, which runs in O(n^2) time is often faster on almost sorted data. Oftentimes, the constant factors might make an algorithm too slow to be practical. Remember, big-O deals with things that are more efficient in the limit, rather than in the immediate case. An algorithm that is O(1) with a constant factor of 10000000 will be significantly slower than an O(n) algorithm with a constant factor of 1 for n < 10000000.

superlizardmo
- 333
- 1
- 11
-
1There has to be some part of `for all of n` that you choose to ignore. – greybeard Oct 27 '16 at 21:30
-
@greybeard it's not clear to me whether the questioner means they *measured* the O(1) algorithm vs. the O(n) one, or they just *assume* O(1) is always less than O(n). It's a very poorly worded question. – Mark Ransom Oct 27 '16 at 21:45
-
(@MarkRansom I take the question to be an assignment shortly after an introduction to Big-O-notation.) – greybeard Oct 27 '16 at 21:55