Couple very basic time complexity related questions here:
What is the time complexity of array initialization in java? e.g.,
int arr[] = new int[5000];
I am guessing it to be O(n) or does the JVM invokes some crazy hardware voodoo magic that it takes only O(1)?
What about inserting or retrieving elements using array index?
void setNumber(int number, int arrIndex) { arr[arrIndex] = number; } int getNumber(int arrIndex) { return arr[arrIndex]; }
I am guessing this to be O(1). But say if arrIndex is 4999 then, wouldn't the pointer that is pointing to the head of the array calculates that the item to be retrieved is at head+(size of int)*arrIndex and 'moves' n (~5000) position to retrieve that item thus making the complexity O(n) and not O(1).
Perhaps we do not consider things at hardware level and conveniently call it the 'constant' associated with the time complexity? Could someone please clarify? Thanks!