TIME COMPLEXITY BEST CASE TIME COMPLEXITY FOR INSERTION IS O(1) as the element to be inserted is at last index. So the element is directly inserted WORST CASE TIME COMPLEXITY FOR INSERTION IS O(n) as the element to be inserted is at first index. So firstly all the elements are shifted then new element is inserted */
| 45 | WORST CASE TIME COMPLEXITY FOR INSERTION IS O(n) as the element to be inserted is at first index. So firstly all the elements are shifted then new element is inserted |
| 46 | */ |
| 47 | int insert(int arr[], int element, int index, int size, int capacity) |
| 48 | { |
| 49 | // Check whether array is Full or Empty |
| 50 | if (size >= capacity) |
| 51 | { |
| 52 | return -1; // -1 Indicates unsuccessfull insertion |
| 53 | } |
| 54 | |
| 55 | else |
| 56 | { |
| 57 | // For loop to move elements forward so that new element can be inserted |
| 58 | for (int i = size - 1; i >= index; i--) |
| 59 | { |
| 60 | // Shift the elements for insertion |
| 61 | arr[i + 1] = arr[i]; |
| 62 | } |
| 63 | arr[index] = element; // Put the element at required index |
| 64 | cout << "After Insertion: " << endl; |
| 65 | return 1; |
| 66 | } |
| 67 | } |
| 68 | //-------------------------------------------------------------------------------------------------------------------------------------- |
| 69 | |
| 70 | //-------------------------------------------- DELETION in ARRAY ------------------------------------------------------------------------ |