As usually reading through stuff I got this question in mind. If I have a character array initialization like below , what will be the time complexity for this statement?
char array[] = {'a','b','c','d','e'};
Will it actually execute 5 time to assign each variable; as we do in loop. If assumption is true why is it so?
depending on where the declaration occurs and the compiler's intelligence, it could be O(1) or O(n). some cases:
It's O(N). It might be a fast or efficient O(N). And if it's a statically allocated array, the initialization will only be done once.
But in any case, it's O(N).
Note that even if it's an array that's placed in the binary image of the program (because the compiler determines that the array is never modified), it's still an O(N) operation to initialize it, even though the initialization might occur before the program event reaches main() or is done as part of the program image load.
It's O(N) because whatever does the initialization has to write to each location of the array, so an array that's 100 times as long as another will have on the order of 100 times more operations to perform to complete the initialization.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With