0

Theory vs practice here. Regarding time complexity, and I have a conceptual question that we didn't get to go deeper into in class.

Here it is:

There's a barbaric BROOT force algorithm, O(n^3)... and we got it down o O(n) and it was considered good enough. If we dive in deeper, it is actually O(n)+O(n), two separate iterations of the input. I came up with another way which was actually O(n/2). But those two algorithms are considered to be the same since both are O(n) and as n reaches infinity, it makes no difference, so not necessary at all once we reach O(n).

My question is:

In reality, in practice, we always have a finite number of inputs (admittedly occasionally in the trillions). So following the time complexity logic, O(n/2) is four times as fast as O(2n). So if we can make it faster, why not?

1
  • 1
    O(n)+O(n) is the same as O(n), and is also the same as O(n/2). Asymptotic analysis ignores such constants. What you mean is, suppose there is another O(n) algorithm with a lower hidden constant. And indeed, why not use a faster algorithm instead of a slower one? I don't see that there is a question here, but to know which O(n) algorithm is faster you will need some other method of analysis than asymptotic analysis; an algorithm is not automatically faster just because it has fewer loops. Commented Feb 23, 2020 at 14:37

1 Answer 1

1

Time complexity is not everything. As you already noticed, the Big-Oh can hide a lot and also assumes that all operations cost the same.

In Practice you should always try to find a fast/the fastest solution for your problem. Sometimes this means that you use a algorithm with a bad complexity but good constants if you know that your problem is always small. Depending on your use case, you also want to implement optimizations that utilize hardware properties like cache optimizations.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.