I am following an online course about data structures and algorithms. In that course, the instructor tells that the time complexities of following ways are different.
Method 1:
Declare:
int arr[]------------>O(1)
Instantiation:
arr = new int[size]------>O(1)
Initialization:
arr[0]=0;------------>O(1)
-------------->O(n)
arr[1]=1;------------>O(1)
Method 2:
Declaration,instantiation and initialization:
int arr[]={10,20,30}---------------->O(1)
I need to know that by following the second method can we optimise our program and how can it possible to tell that it's having O(1)
,what's the difference between both these methods.
I mean that I think although the second method is having fewer steps it internally follows all the steps which are in the first method, so it can't be O(1)
it's also O(n)
, Iam I correct?