Why is accessing any single element in an array done in constant time ( O(1) )?

后端 未结 5 815
灰色年华
灰色年华 2020-12-14 04:32

According to Wikipedia, accessing any single element in an array takes constant time as only one operation has to be performed to locate it.

To me, what happens behi

5条回答
  •  [愿得一人]
    2020-12-14 04:50

    In theory elements of an array are of the same known size and they are located in a continuous part of memory so if beginning of the array is located at A memory address if you want to access any element you have to compute its address like this:

    A + item_size*index so this is a constant time operation.

提交回复
热议问题