Data Structure Array
Data Structure Array
Algorithms
Module -3
Array
Dr. R. Jothi, SCOPE
VIT Chennai
Outline
Types of Data Structure
Array Introduction
Matrices
}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
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
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
ThreeD[1][1][2]