Index of Element in Array (Permutation)
The following algorithm finds the index of an element in an array of integers, or None if it is not found.
def find_element_index(a, x):
# Get length of array
n = len(a)
# Iterate over all elements
for i in range(n):
# Get value of element
val = a[i]
# Check element found
if val == x:
# Return current index as found
return i
# Return not found
return None
If the array is a permutation of the numbers from 0 to n - 1, and the element to find is within that range, the following is requested:
(a) Find the expression for the expected value of the growth order in terms of the size n of the array.
(b) The expression of the growth order according to the asymptotic notation O().
(c) How would you reconcile the two previous answers?