MCPcopy Create free account
hub / github.com/Lakhankumawat/LearnCPP / insert

Function insert

A-Array/arrayOperations.cpp:47–67  ·  view source on GitHub ↗

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 */

Source from the content-addressed store, hash-verified

45WORST 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*/
47int 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 ------------------------------------------------------------------------

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected