3 ms·
Constant time means the time, expressed in the number of steps the algorithm takes, doesn't vary based on the size of the input. The time to reach the relevant
by vinceguidry 11y ago
Constant time means the time, expressed in the number of steps the algorithm takes, doesn't vary based on the size of the input. The time to reach the relevant memory doesn't vary on the size of the input, but rather on the topology of your architecture. Further elements may well take less time, depending on where they're actually stored, so you can simply average out memory access to get a constant factor.
Now, if the number of times you had to access the memory in order to retrieve the data in the array depended on how big the array was, then you'd have O(N). An array is specifically designed so you only have to go to memory twice, first to get the memory address stored at X position in the array, then to retrieve the information stored on that position.
A linked list, as described in a sibling comment, actually does have to do this. You trade easy accessibility for other properties.
Ditto for the steps to do register operations and any of the other machine language instructions. The time can vary, but not as a function of the size of the input. So it's still constant time.