Time Complexity Of Accessing An Element In A Tuple
Solution 1:
It's O(1) for both list and tuple. They are both morally equivalent to an integer indexed array.
Solution 2:
Lists and tuples are indexable in the exact same way arrays are in other languages.
A simplified explanation is that space is allocated for references to objects, those references take up a uniform amount of space, and any index is simply multiplied by the size of the reference to get an offset into the array. This gives constant, O(1), access for lists and tuples.
Solution 3:
Getting an item from a linked-list is O(n), but Python lists have array-based implementations so the cost is O(1).
Tuples are also implemented using arrays so it's O(1) for them too.
Solution 4:
It should be O(1)
, because it really is only a list.
But for python lists, I'd expect O(1)
too! You might want to think about it again...
Post a Comment for "Time Complexity Of Accessing An Element In A Tuple"