Data Structures and
Algorithms
Module -3
Array
Dr. R. Jothi, SCOPE
VIT Chennai
Outline
Types of Data Structure
Array Introduction
Matrices
Dr. R. Jothi, VIT Chennai
Types of Data structures
Linear
data elements in a linear data structure are traversed
one after the other (sequential)
Data items can be traversed in a single-run
E.g. Array, Linked List, Queue, Stack
Non-Linear
data items are not stored sequentially
No-single level traversal visits all the elements
E.g. Tree, Graph
Data structure operations
1. Inserting
2. Deleting
3. Updating
4. Traversing
5. Searching
ADT – Abstract Data Type (Data + Operations)
Array
Collection of homogenous elements
E.g. A set of cricket scores obtained by Virat Kohli
in last 5 matches
List of students, employees, etc.
Every student/employee has same structure
Dr. R. Jothi, VIT Chennai
Dr. R. Jothi, VIT Chennai
Arrays of structures
Example:
struct contact{
#name, phoneno, email..
}cc[100];
John
12345 M
COMP
...
0 1 2 … 98 99 7
2D-Array
Tables, Spreadsheets, Matrices, etc.
Indexed by Rows and Columns
Locating an element in 2D array
In Row-major order
A[row][column] can then be computed as:
offset = row*NUMCOLS + column
Location of A[row][col]=Base address + offset*datatype_size
Assuming index starts from 0, i.e. row 1, column 2 in matrix A,
would be represented as A[0][1]
Locating an element in 2D array
In Column-major order
A[row][column] can then be computed as:
offset = row + column*NUMROWS
Location of A[row][col]=Base address + offset*datatype_size
Assuming index starts from 0, i.e. row 1, column 2 in matrix A,
would be represented as A[0][1]
Linear List using Array representation
Individual element is located in the array using a mathematical
formula
location(i) = i – 1
ith element of the list is in index i-1 of the array
Insert at end
Data elements : 5,99,23,44,1,7,2,9
0th 5 5 5
element
Insert 99 99
99
5 23
44
5th
1
element 7
count = 0 count = 1 count = 2
count =
6,
Overfull
Insert at position k
0th 5 5 5
element 23
Insert 0 99 99 99
@3
5@1 0 @2 0 23
0 0 0
0 0 0
5th
element 0 0 0
count = 0 count = 1 count = 2
5 5 5
88 99 99 88
@2 23 99 99
23 23 23
0 0 0
0 0 0
Steps: Insert at position k
1. Start from current size of the array (count) to position, shift
elements to right.
2. Insert element at index p-1
Operation Insert at position k (index
k-1)
insert(int element[], int k, int x)
{
if (k < 0 || k > count) error;
if (count == MAX) error;
for (i = length-1; i >= k; i--)
element[i+1] = element[i];
element[k-1] = x;
count++;
}
The time complexity is O(length))
Delete sequence : 99, 88,1
0th 5 5 5 5
element
99 Delete 23 23 1 23
88
23 99 44 44 44
44 1 1 7
1 7 7 0
5th
element 7 0 0 0
count = 6 count = 5 count = 5 count = 4,
Not
found!!!
Operation Delete
delete(int element[ ], int x)
{
// Delete the element x if it exists.
k=Search(x) ;
If(k>0)
{
for (int i = k; i < length; i++)
element[i-1] = element[i];
length--;
}
}
• The time complexity is O(length)
Operation Search
int Search( int element[ ], int x )
{
// Locate x and return the position of x if found
for (int i = 0; i < length; i++)
if (element[i] == x)
return ++i;
return 0;
}
• The time complexity is O(length)
Operation Traverse
(Output)
Output(element [])
{
// print out the list
for (int i = 0; i < length; i++)
printf(“%d” , element[i] );
}
• The time complexity is O(length)
3D Array
Dr. R. Jothi, VIT Chennai
What does
this program
prints? Type
your answer
in chat box
Dr. R. Jothi, VIT Chennai
How do you
access 15 in
ThreeD array?
ThreeD[1][1][2]
Dr. R. Jothi, VIT Chennai
1D vs. 2D vs. 3D Array
Dr. R. Jothi, VIT Chennai