Big O Time Complexity of effectively equivalent statements
Suppose you have algorithm 1 (initializing every element in an
instantiated array to 0):
intArray[0] = 0; intArray[1] = 0; ... intArray[intArray.length - 1] = 0;
and algorithm 2:
for( int i = 0; i < intArray.length; i++) intArray[i] = 0;
Are their time complexities equal? I was taught to consider comparisons
and assignments, and it seems to me that algorithm 1 would have
(intArray.length) fewer comparisons than algorithm 2 and would therefore
take half the time.
No comments:
Post a Comment