I know that if I have a function like:
public int addOne(int a){
return (a+1)
}
The time complexity order will be O(1) since we only do one operation (the sum).
But what if I have a function that doesn't do any operations, just assigns some values to some global variables. Like this:
public void assignValues(){
a = 2;
b = 3;
c = 4;
//maybe more
}
What would the time complexity be for this function? My guess is that it would still O(1). Is that correct?
When you discuss the time complexity of an algorithm, you first have to define the variable parameter(s). For example, it doesn't make any sense to say that something is
O(n)
without defining what you measure byn
(e.g. the length of an array? The size of the contents of an array? The bit-length of a number? The absolute value of an integer?).In your particular case, you have a function that takes no parameters. Assuming that the operations within the function don't depend on any other external parameters, the complexity of such a function is always trivially
O(1)
, no matter what operations you perform inside. For example, the following function is alsoO(1)
:As already mentioned, this assumes that the parameter-less function has no external dependencies. Consider e.g. the following function
This function is
O(n log n)
ifn
is the amount of time that has passed since 1.1.0001.