It is possible to state that the cost of an O(n²) algorithm will always be less than a cost of O(n). Comment on the truth of the statement:
1 Answer
No. Saying that an algorithm is O(n²) means that there is at least a case where the algorithm will take a quadratic number of operations to finish.
In a O(n) algorithm, the worst case will be linear, so for a big enough input, the worst case of a O(n²) algorithm will always take more operations than a O(n) algorithm.
O(n) = k * n + a
O(n²) = k2 * n² + k1 * n + b
so we wanna prove that there is a case that O(n) < O(n²)
k * n + a < k2 * n² + k1 * n + b
(k - k1) * n + (a - b) < k2 * n²
(k - k1) / k2 + (a - b) / (k2 * n) < n
We can see that, as n grows, the constants in the left side will stay the same or decrease, so there will be a point where O(n) < O(n²).
O(x)algorithm, the complexity (e.g. time or space complexity) grows likex. Maybe in your case it helps to draw the two functionsf(n) = n²andg(n) = nand then derive if the statement is true or not?