0% found this document useful (0 votes)
9 views

Data Structure 11 (1)

The document explains the concept of insertion in data structures, specifically focusing on how to add elements to an array at various positions, including the beginning, a specified index, after a given index, and before a specified index. It provides algorithms and example code snippets for each insertion scenario, demonstrating how existing elements must be shifted to accommodate the new element. The document also includes sample outputs to illustrate the results of the insertion operations.

Uploaded by

Anshuman Ravi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Data Structure 11 (1)

The document explains the concept of insertion in data structures, specifically focusing on how to add elements to an array at various positions, including the beginning, a specified index, after a given index, and before a specified index. It provides algorithms and example code snippets for each insertion scenario, demonstrating how existing elements must be shifted to accommodate the new element. The document also includes sample outputs to illustrate the results of the insertion operations.

Uploaded by

Anshuman Ravi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

DATA STRUCTURE

INSERTION
PRESENTED BY

ANJALI SHARMA
WHAT IS INSERTION ?
 Insertion is operation in which a new value is added at a particular
in an array.
 Insertion an element at the “end” of a linear array can be easily
done provided the memory space allocated for the array is large
enough to accommodate the additional element.
 Let A be a collection of data elements in the memory of the
computer. Insertion refers to the operation of adding elements to
the collection.
 If an element is to the inserted in the middle of the array, then, on
the average, half of the elements must be moved downward to
new locations to accommodate the new elements and keep the
order of the other elements.
Consider the list given below:

Price 0 1 2 3 4
Items: 10 20 30 40 50
Now the item 15 is to be inserted in position 1
First move item 50 from position 4 to position 5
Then move item 40 from position 3 to position 4
Then move item 30 from position 2 to position 3
Then move item 20 from position 1 to position 2
Now position 1 becomes vacant.
 INSERTION AT THE BEGINNING
OF AN ARRAY

WHEN THE INSERTION HAPPENS AT THE BEGINNING, IT CAUSES


ALL THE EXISTING DATA ITEMS TO SHIFT ONE STEP DOWNWARD.
HERE, WE DESIGN AND IMPLEMENT AN ALGORITHM TO INSERT AN
ELEMENT AT THE BEGINNING OF AN ARRAY.
PRINTF("PRINTING ARRAY BEFORE
INSERTION −\N");
FOR(I = 0; I < N; I++) {
PRINTF("ARRAY[%D] = %D \N", I,
ARRAY[I]);
}
// NOW SHIFT REST OF THE ELEMENTS DOWNWARDS
FOR(I = N; I >= 0; I--) {
ARRAY[I+1] = ARRAY[I];
}
// ADD NEW ELEMENT AT FIRST POSITION
ARRAY[0] = VALUE;
// INCREASE N TO REFLECT NUMBER OF ELEMENTS
N++;
// PRINT TO CONFIRM
PRINTF("PRINTING ARRAY AFTER INSERTION −\N");
FOR(I = 0; I < N; I++) {
PRINTF("ARRAY[%D] = %D\N", I, ARRAY[I]);
}
}
OUT PUT
• PRINTING ARRAY BEFORE INSERTION −
• ARRAY[0] = 2
• ARRAY[1] = 3
• ARRAY[2] = 4
• ARRAY[3] = 5
• PRINTING ARRAY AFTER INSERTION −
• ARRAY[0] = 0
• ARRAY[1] = 2
• ARRAY[2] = 3
• ARRAY[3] = 4

 INSERTION AT THE GIVEN INDEX OF AN
ARRAY
• IN THIS SCENARIO, WE ARE GIVEN THE EXACT LOCATION (INDEX) OF AN ARRAY WHERE
A NEW DATA ELEMENT (VALUE) NEEDS TO BE INSERTED. FIRST WE SHALL CHECK IF THE
ARRAY IS FULL, IF IT IS NOT, THEN WE SHALL MOVE ALL DATA ELEMENTS FROM THAT
LOCATION ONE STEP DOWNWARD. THIS WILL MAKE ROOM FOR A NEW DATA ELEMENT.
• PROGRAM:-

#INCLUDE <STDIO.H>
#DEFINE MAX 5
VOID MAIN() {
INT ARRAY[MAX] = {1, 2, 4, 5};
INT N = 4; // NUMBER OF ELEMENTS IN ARRAY
INT I = 0; // LOOP VARIABLE
INT INDEX = 2; // INDEX LOCATION TO INSERT NEW VALUE
INT VALUE = 3; // NEW DATA ELEMENT TO BE INSERTED
// print array before insertion
printf("Printing array before insertion −\n");

for(i = 0; i < N; i++) {


printf("array[%d] = %d \n", i, array[i]);
}
// now shift rest of the elements downwards
for(i = N; i >= index; i--) {
array[i+1] = array[i];
}
// add new element at first position
array[index] = value;

// increase N to reflect number of elements


N++;

// print to confirm
printf("Printing array after insertion −\n");

for(i = 0; i < N; i++) {


printf("array[%d] = %d\n", i, array[i]);
}
}
OUTPUT
• PRINTING ARRAY BEFORE INSERTION −
• ARRAY[0] = 1
• ARRAY[1] = 2
• ARRAY[2] = 4
• ARRAY[3] = 5
• PRINTING ARRAY AFTER INSERTION −
• ARRAY[0] = 1
• ARRAY[1] = 2
• ARRAY[2] = 3
• ARRAY[3] = 4
• ARRAY[4] = 5
 INSERTION AFTER THE GIVEN INDEX OF
AN ARRAY
• IN THIS SCENARIO WE ARE GIVEN A LOCATION (INDEX) OF AN ARRAY AFTER
WHICH A NEW DATA ELEMENT (VALUE) HAS TO BE INSERTED. ONLY THE SEEK
PROCESS VARIES, THE REST OF THE ACTIVITIES ARE THE SAME AS IN THE
PREVIOUS EXAMPLE.
• PROGRAM:-

#INCLUDE <STDIO.H>
#DEFINE MAX 5
VOID MAIN() {
INT ARRAY[MAX] = {1, 2, 4, 5};
INT N = 4; // NUMBER OF ELEMENTS IN ARRAY
INT I = 0; // LOOP VARIABLE
INT INDEX = 1; // INDEX LOCATION AFTER WHICH VALUE WILL BE INSERTED
INT VALUE = 3; // NEW DATA ELEMENT TO BE INSERTED
// print array before insertion
printf("Printing array before insertion −\n");

for(i = 0; i < N; i++) {


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

// now shift rest of the elements downwards


for(i = N; i >= index + 1; i--) {
array[i + 1] = array[i];
}

// add new element at first position


array[index + 1] = value;

// increase N to reflect number of elements


N++;

// print to confirm
printf("Printing array after insertion −\n");

for(i = 0; i < N; i++) {


printf("array[%d] = %d\n", i, array[i]);
}
}
OUTPUT
• PRINTING ARRAY BEFORE INSERTION −
• ARRAY[0] = 1
• ARRAY[1] = 2
• ARRAY[2] = 4
• ARRAY[3] = 5
• PRINTING ARRAY AFTER INSERTION −
• ARRAY[0] = 1
• ARRAY[1] = 2
• ARRAY[2] = 3
• ARRAY[3] = 4
• ARRAY[4] = 5
 INSERTION BEFORE THE GIVEN INDEX OF AN
ARRAY
• N THIS SCENARIO WE ARE GIVEN A LOCATION (INDEX) OF AN ARRAY BEFORE
WHICH A NEW DATA ELEMENT (VALUE) HAS TO BE INSERTED. THIS TIME WE SEEK
TILL INDEX-1 I.E., ONE LOCATION AHEAD OF GIVEN INDEX, REST OF THE ACTIVITIES
ARE SAME AS IN PREVIOUS EXAMPLE.
• PROGRAM:-

#INCLUDE <STDIO.H>
#DEFINE MAX 5
VOID MAIN()
{
INT ARRAY[MAX] = {1, 2, 4, 5};
INT N = 4; // NUMBER OF ELEMENTS IN ARRAY
INT I = 0; // LOOP VARIABLE
INT INDEX = 3; // INDEX LOCATION BEFORE WHICH VALUE WILL BE INSERTED
// print array before insertion
printf("Printing array before insertion −\n");
for(i = 0; i < N; i++) {
printf("array[%d] = %d \n", i, array[i]);
}
// now shift rest of the elements downwards
for(i = N; i >= index + 1; i--) {
array[i + 1] = array[i];
}
// add new element at first position
array[index + 1] = value;

// increase N to reflect number of elements


N++;

// print to confirm
printf("Printing array after insertion −\n");

for(i = 0; i < N; i++) {


printf("array[%d] = %d\n", i, array[i]);
}
}
OUTPUT
• PRINTING ARRAY BEFORE INSERTION −
• ARRAY[0] = 1
• ARRAY[1] = 2
• ARRAY[2] = 4
• ARRAY[3] = 5
• PRINTING ARRAY AFTER INSERTION −
• ARRAY[0] = 1
• ARRAY[1] = 2
• ARRAY[2] = 4
• ARRAY[3] = 5
• ARRAY[4] = 3

You might also like