Docsity

Insertion Operation on Arrays: Algorithm and Implementation in C, Lecture notes of Data Structures and Algorithms

Partial preview of the text

Download Insertion Operation on Arrays: Algorithm and Implementation in C and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Data Structure and

Algorithms

Insertion Operation on Arrays

int array[] = {1,2,3,4}; Elements Index

Insert 5 at index 2

Elements Index 1 2 3 4 0 1 2 3 1 2 5 3 4 0 1 2 3 4

Implementation of the Algorithm

int LA[] = {1,3,5,7,8};

int item = 10, k = 3, n = 5, j = n;

int i = 0;

//Declaration of the variables to be used

Implementation of the Algorithm

printf("The original array elements are :\n");

for(i=0;i<n;i++)

printf("LA[%d] = %d\n",i,LA[i]);

//Displaying the array before insertion

Implementation of the Algorithm

while( j >= k)

LA[j+1] = LA[j];

j = j - 1;

//Moving the elements

Implementation of the Algorithm

LA[k] = item; //Insertion of the new element on the given index

If we compile and run the

program …

Data Structure and

Algorithms