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

An explanation of the insertion operation on arrays using an algorithm and its implementation in C programming language. The algorithm is described step by step, and the C code snippet demonstrates how to insert an element at a given index in an array. The document also includes the output of the program before and after the insertion.

Typology: Lecture notes

2019/2020

Uploaded on 06/26/2020

bachie-babe
bachie-babe 🇵🇭

1 document

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structure and
Algorithms
pf3
pf4
pf5
pf8
pf9
pfa

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