| 19 | arr->A[arr->length++] = x; // increment the size of array and fit the elements // it will take the constant time |
| 20 | } |
| 21 | void Insert(struct Array* arr, int index, int x) // the index at which we want to enter // and the value we want tpo enter |
| 22 | { |
| 23 | int i; |
| 24 | for (i = arr->length; i > index; i--) // we will strat from last of the array and shift the elements upto the given index |
| 25 | { |
| 26 | arr->A[i] = arr->A[i - 1]; // shifting the elements to next // i is on some index and copy the value from previous index |
| 27 | } |
| 28 | arr->A[index] = x; // set the value at the particular index |
| 29 | arr->length++; // increment the size of length |
| 30 | // work done shifting of elelmets and copying elements |
| 31 | // best case : --------------- min : O(1) // there may be zero shifting |
| 32 | // worst case : --------------- max : O(n)// there may be n shifing |
| 33 | |
| 34 | } |
| 35 | |
| 36 | int main() { |
| 37 | int no; |