I'm learning about Big O notation and how it relates to computer science and I understand the sentiment behind Big O, however I'm struggling at understanding how we define a "step".
In the book I'm reading as well as a lot of different articles they use something like below to express an O(N) example since, as N increases we perform more steps linearly:
void printAllElementOfArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d\n", arr[i]);
}
}
I understand the purpose of the example wherein for the function above "we perform printf N times" and therefore the algorithm is best represented through f(N) <= O(N) (I think that's the mathematical meaning, I get the sentiment of Big O but not fully there yet on the math).
The piece I'm really struggling with though is why we consider "printf" a single step. printf is in itself its own function with its own sequence of steps and so to that end, does that function not need to be elaborated? Is there any defined criteria of when an inner function is elaborated vs when we define it as a unit step?
I read somewhere that usually for nested functions big O is the multiplication of steps for each function O(g(N) x f(N)).
- So to that end, is the implication/assumption here that printf is O(1) and therefore the overall algorithm is O(1*N)?
Any clarifications is really appreciated.
printf
isO(1)
, but without seeing the book/context, I'm skeptical that they didn't either A) Make a mistake, or B) Intend N to mean "the combined input size". I.e., N may not refer to "array length", but total number of characters to print.O(N)
makes perfect sense if you calculate according to B. – svidgen Jan 23 '23 at 15:27_ThorbjørnRavnAndersen, yeah I didn't edit the question, not sure why it says I did, I'm new though so maybe I opened it.
@svidgen, so just for reference / to answer your question, the book I'm referencing (Data Structures and Algorithms by Jay Wengrow, pg 44) and Im having a tough time writing the code but its just, listing the array (N), looping through the array and printing the result and considers it O(N)
– Davis Kim Jan 25 '23 at 01:05