MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / Insert

Function Insert

Arrays/05_inserting_an_element.cpp:21–34  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19 arr->A[arr->length++] = x; // increment the size of array and fit the elements // it will take the constant time
20}
21void 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
36int main() {
37 int no;

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected