Week 1 - Operation in Array
Week 1 - Operation in Array
LINEAR ARRAY
Aditya Tiwari
Assistant Professor
CSVTU BHILAI
•What Are Arrays in Data Structures?
•An array is a linear data structure that collects
elements of the same data type and stores them
in contiguous and adjacent memory locations.
Arrays work on an index system starting from 0
to (n-1), where n is the size of the array.
•It is an array, but there is a reason that arrays
came into the picture.
What Are Arrays in Data Structures?
Why Do You Need an Array in Data
Structures?
• Let's suppose a class consists of ten students, and the
class has to publish their results. If you had declared
all ten variables individually, it would be challenging to
manipulate and maintain the data.
• If more students were to join, it would become more
difficult to declare all the variables and keep track of
it. To overcome this problem, arrays came into the
picture.
What Are the Types of Arrays?
• There are majorly two types of arrays, they are:
• One-Dimensional Arrays:
• You can imagine a 1d array as a row, where elements are stored one
after another.
Multi-Dimensional Arrays:
These multi-dimensional arrays are again of two types.
They are:
• Two-Dimensional Arrays:
• You can imagine it like a
table where each cell
contains elements
Three-Dimensional Arrays:
You can imagine it like a cuboid made up of smaller
cuboids where each cuboid can contain an element.
How Do You Declare an Array?
• int arr[5];
• arr[0]=1;
• arr[1]=2;
• arr[2]=3;
• arr[3]=4;
• arr[4]=5;
How Can You Access Elements of Arrays in Data Structures?
• You can access elements with the help of the index at which you stored them. Let's discuss it
with a code:
• #include<stdio.h>
• int main()
• {
• int a[5] = {2, 3, 5, 7, 11};
• printf(“%d\n”,a[0]); // we are accessing
• printf(“%d\n”,a[1]);
• printf(“%d\n”,a[2]);
• printf(“%d\n”,a[3]);
• printf(“%d”,a[4]);
• return 0;
• }
What Operations Can You Perform on an Array?
• Traversal
• Insertion
• Deletion
• Searching
• Sorting
Traversing the Array: Traversal in an array is a process of
visiting each element once.
• Here LA is a linear array with lower bound LB and
upper bound UB.
A B C D E G H I
• LB 1 2 3 4 5 6 7 8 UB 9 10
• LB 1 2 3 4 5 6 7 8 9 10
• Step 3: Inset F into Kth position
K
A B C D E F G H I
• LB 1 2 3 4 5 6 7 8 UB 9 10
• Step 1: Set J= N
• Step 2: Repeat step 3 and 4 while J >= K.
• Step 3: Set LA [J+1]= LA [J] [Move Jth elements downwards]
• Step 4: Set J= J – 1. [Decrease counter]
• [End of step 2 loop]
• Step 5: Set LA [K] = ITEM
• Step 6: Set N=N + 1 [Reset N]
• Step 7: Exit.
Deletion: IN A LINEAR ARRAY
• Deletion of an element is
the process of removing the
desired element and re-
organizing it.
• You can also do deletion in
different ways:
• At the beginning
• At the end
• At any Position element
• Example :
• Step 1: Given array with elements
A B C D E F G H I
• LB 1 2 3 4 5 6 7 8 UB 9 10
• LB 1 2 3 4 5 6 7 8 UB 9 10
• Step 3: Inset F into Kth position
K
A B C D E G H I
• LB 1 2 3 4 5 6 7 UB 8 9 10
DELETION PSEUDO CODE:-
• Here LA is a linear array with N elements and K is a positive
integer such that K<=N. This algorithm deletes the Kth
elements from LA.
• Step 1: Set Item = LA [K]
• Step 2: Repeat for J= K to N – 1
• Set LA [J] = LA [J + 1] [Move J + 1st element upword]
• [End of loop]
• Step 3: Set N = N – 1. [Reset N]
• Step 4: Exit.