Unit - 1 - Array in DS
Unit - 1 - Array in DS
Properties of Array
Each element of an array is of same data type and carries the same size, i.e.,
int = 4 bytes.
Elements of the array are stored at contiguous memory locations where the
first element is stored at the smallest memory location.
Elements of the array can be randomly accessed since we can calculate the
address of each element of the array with the given base address and the size
of the data element.
Advantage of C Array
2) Ease of traversing: By using the for loop, we can retrieve the elements of an
array easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines of code
only.
4) Random Access: We can access any element randomly using the array.
Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of declaration of the array, we
can't exceed the limit. So, it doesn't grow the size dynamically like LinkedList
which we will learn later.
Declaration of C Array
data_type array_name[array_size];
int marks[5];
Here, int is the data_type, marks are the array_name, and 5 is the array_size.
Initialization of C Array
The simplest way to initialize an array is by using the index of each element. We
can initialize each element of the array by using the index. Consider the following
example.
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
Representation of an array
We can represent an array in various ways in different programming languages.
As per the above illustration, there are some of the following important points -
In c programming language, single dimensional arrays are used to store list of values of
same data type.
In other words, single dimensional arrays are used to store a row of values.
In single dimensional array, data is stored in linear form. Single dimensional arrays are
also called as one-dimensional arrays, Linear Arrays or simply 1-D Arrays.
We use the following general syntax for declaring a single dimensional array...
data_type array_name[array_size];
arrayName[indexvalue]
example
marks[2] =80;
Then address of the ith element can be find out using following formulas
Loc(a[i]) = BA + (i – Lb) * C
Example: Suppose following array with its base address 1000 is given then
address of the element at a[4] will be 1008,
= 1000 + ( 4 – 0 )*2
= 1000 + 8
= 1008
In simple words, an array created with more than one dimension (size) is called as
multi dimensional array.
Multi dimensional array can be of two dimensional array or three dimensional
array or four dimensional array or more...
Most popular and commonly used multi dimensional array is two dimensional
array.
The 2-D arrays are used to store data in the form of table. We also use 2-D arrays
to create mathematical matrices.
We use the following general syntax for declaring a two dimensional array.
datatype arrayname[rowsize][columnsize];
Example
int matrix[2][3]
Example
arrayName[rowindex][colindex];
Example
matrix[0][1] = 2 ;
Address of an element of any array say “A[ I ][ J ]” is calculated in two forms as given:
(1) Row Major System (2) Column Major System
The address of a location in Row Major System is calculated using the following formula:
Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]
The address of a location in Column Major System is calculated using the following formula:
Where,
B = Base address
Lr = Lower limit of row/start row index of matrix, if not given assume 0 (zero)
Lc = Lower limit of column/start column index of matrix, if not given assume 0 (zero)
Example: If we need to calculate the average of the marks obtained by a student in 6 different
subject, we need to traverse the complete array of marks and calculate the total sum, then we will
devide that sum by the number of subjects i.e. 6, in order to find the average.
2) Insertion: Insertion can be defined as the process of adding the elements to the data structure
at any location.
If the size of data structure is n then we can only insert n-1 data elements into it.
3) Deletion: The process of removing an element from the data structure is called Deletion. We
can delete an element from the data structure at any random location.
If we try to delete an element from an empty data structure then underflow occurs.
4) Searching: The process of finding the location of an element within the data structure is
called Searching. There are two algorithms to perform searching, Linear Search and Binary
Search. We will discuss each one of them later in this tutorial.
5) Sorting: The process of arranging the data structure in a specific order is known as Sorting.
There are many algorithms that can be used to perform sorting, for example, insertion sort,
selection sort, bubble sort, etc.
6) Merging: When two lists List A and List B of size M and N respectively, of similar type of
elements, clubbed or joined to produce the third list, List C of size (M+N), then this process is
called merging
1) Traversing an Array
Traversal operation in array or simply traversing an array means, Accessing or printing each
element of an array exactly once so that the data items (values) of the array can be checked or
used as part of some other operation or process (This accessing and processing is sometimes
called “visiting” the array).
#include<stdio.h>
void main()
int i ;
for(i=0;i<5;i++)
2) Insertion in Array
Insertion in the operation in which a new value is added at a particular place in an array.
Input
Output
Input
Output
"Invalid Position"
Algorithm
4. If it is valid,
Shift all the elements from the last index to position index by 1 position to the right.
5. Otherwise,
Invalid Position
Algo
Step 1: for i = N-1(index of last element in array) down to Pos repeat step 2
Step 4: N = N + 1
Example
1. We need to insert element 100 at position 2.
2. Move all the elements from the last index(4) to the position(2) to one position right.
3) Deletion in Array
Deletion operation in Array or delete an element from an Array simply means, Removing an
existing element from a specific position of an array.
The following algorithm deletes a data element from a specific position pos (position specified
by the programmer) of a linear array „a„.
Step 04: [Move ith element backward (left). ] set a[i] = a[i+1]
Algorithm
1. Find the given element in the given array and note the index.
2. If the element found,
Shift all the elements from (index + 1) by 1 position to the left.
Reduce the array size by 1.
3. Otherwise, print "Element Not Found"
Example
Let's take an array of 5 elements.
1, 20, 5, 78, 30.
If we remove element 20 from the array, the execution will be
Note – Merging, Searching and sorting operations are discussed in separate chapter