An array is a linear information construction. In an array, the operation to fetch a price takes fixed time i.e., O(1). Allow us to see why is it so.
Why the complexity of fetching worth is O(1)?
As Arrays are allotted contiguously in reminiscence, Fetching a price by way of an index of the array is an arithmetic operation. All arithmetic operations are accomplished in fixed time i.e., O(1).
Rationalization:
In an array, we now have the reminiscence handle of index 0 (Base handle). By including the product of the index quantity (of worth to be fetched) and the scale of 1 ingredient (ex. int dimension is 4 bytes) with the bottom handle, we will have the handle of that index’s worth. we don’t need to iterate by means of the array. So it’s accomplished in O(1).
Tackle of ith Index = Base handle + offset = Tackle of 0th Index + i × (dimension of 1 ingredient)
Instance:
In array A[] = {8, 6, 7, 13, 8, 19}
To fetch the worth at index 4, we’d like the reminiscence handle the place the worth of that index is saved. The handle may be obtained by doing an arithmetic operation i.e.
Tackle of worth at index 4 = Tackle of index 0’s worth + 4 × dimension of int = 108 + 4 × 4 bytes
Tackle of worth at index 4 = 124
A[4] = worth at handle 124 = 8