Given the following algorithm.
Insert(A,x)
// A is an array and x is of the type element of that array
- i = A.size()
- A.push_back(x)
- While i > 0 and A[i-1] > x
- A[i] = A[i-1]
- i = i - 1
- EndWHile
- A[i] = x
- [10 points] The purpose of this function is to maintain an ordered array. Argue that this algorithm achieves this goal or given an example where this will not be the case..
- [10 points] Will the index i ever be out of bounds? Given an example where this can occur or argue that it can not occur.
- [2 points] What is the performance of this algorithm?