0% found this document useful (0 votes)
15 views6 pages

Understanding Array Data Structures

The document provides an overview of linear data structures, focusing specifically on arrays. It explains the characteristics, representation, and operations of 1-D and 2-D arrays, including insertion, deletion, traversal, and searching. Additionally, it discusses applications of arrays, such as in sparse matrix representation and various algorithms.

Uploaded by

bhattshruti2201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Understanding Array Data Structures

The document provides an overview of linear data structures, focusing specifically on arrays. It explains the characteristics, representation, and operations of 1-D and 2-D arrays, including insertion, deletion, traversal, and searching. Additionally, it discusses applications of arrays, such as in sparse matrix representation and various algorithms.

Uploaded by

bhattshruti2201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Data Structure: Array

Linear data Structures


Introduction:
Based on how data are stored in memory, data structures can be classified in to two types.

 Linear data structures


 Non-linear data structures.

In this unit we will see various linear data structures.

In linear data Structure, data are stored in contiguous (may not physically) memory locations.
Array, stack, queue, and linked list are example of linear data structures. Brief description of all
linear data structures is given in below table:

Type Description Examples / Variants


Array Fixed-size collection of elements stored in 1D Array, 2D Array, Multidimensional
contiguous memory locations. Array
Linked Collection of nodes, where each node Singly Linked List, Doubly Linked List,
List contains data and a pointer to the next. Circular Linked List
Stack Follows LIFO (Last In First Out) principle. Array-based Stack, Linked List-based
Insertion and deletion at top. Stack
Queue Follows FIFO (First In First Out) principle. Simple Queue, Circular Queue, Double-
Insertion at rear, deletion at front. Ended Queue (Deque), Priority Queue

2.1 Array
An array is a series of elements of the same type placed in contiguous memory locations that
can be individually referenced by adding an index to a unique identifier. It is static, random
access data structure and elements are stored in physical contiguous locations.
Array can be 1-D, 2-D or n-D. Depending on language and platform, representation and
storage may vary. Let‟s see 1-D array and 2-D array in c language.

1-D array representation


In c language 1-D array can be declared by many ways.

1) Simple Declaration:
datatype name [size];
eg. int array[10];
Here, in this example, array of type integers is declared statically. Here it can store maximum
10 int value. Here all element of array will be zero or garbage as shown below.

Array 0 -111 0 245 0 -134 0 0 0 0

By K.P. Kandoriya Page 1


Data Structure: Array
2) Value can be assigned along with array declaration as follow.
datatype name[size]={Elements separated by comma };
eg. int array[5]={1,2,3};
Here, values at first three locations are 1, 2 and 3 respectively. And at remaining two
locations, values are zero, as shown below.
array 1 2 3 0 0

3) Array also can be declared without specifying size, as follow.

datatype name[]={Elements separated by comma }


Eg. int array[]={1,2,3,4,5};
Array 1 2 3 4 5

Here, for this array, size is n, it is number of value in between braces ({}). For above example
size is 5.

Operations on 1-D array


Following operations can be performed on 1-D array:
1. Insertion: In array, value at any location can be inserted using index.
For example one can insert element x at index i as in array A (must be declared first)
follow.
A[i]= X;

One can use iteration to insert all elements in sequence as follow.


Here n is size of array.
for(i=0;i<n; i++)
{
Read (x);
A[i]=x;
}
2. Traversal: Traversal of an array means, visit all index and display value in it.
One can use iteration to display all elements in sequence as follow. Here n is size of
array.
for (i=0;i<n; i++)
{
Write (A[i]);
}

3. Searching: In Searching, its checking that weather the given element “e‟ is exist in
array or not. Searching techniques are discussed in unit-6.
4. Merging: Merging means combining two arrays of size n and m, in to one array of
size n + m.
For example a[3]={1,2,3}, b[5]={4,5,6,7,8} can be merge in array c, of size 3+4=7 as
follow.

By K.P. Kandoriya Page 2


Data Structure: Array
C[7]={1,2,3,4,5,6,7,8}, red elements are from a, and black are from b.
5. Deletion: Process of removal of element from index i, from the array is called
deletion. Element cannot be deleted physically from array because array is static data
structure. But you can use rare value to indicate deletion, i.e. it can be just ignored.
For example use -999 to indicate deletion. If value is -999, ignore it. But here if value
-999 is data then also, it will be ignored. So use a value that never going to be stored.
All above operations can be performed on 2-D array.

2-D Array representation and storing


2-d array can be represented in tabular (matrix) form. And it can be declared as follow in c
language.
datatype name[row][colomn];
For example, int a[2][4]={1,2,3,4,5,6,7,8};

#of column=4

# of row=2 1 2 3 4
5 6 7 8

In This example, elements will be stored in contiguous physical locations as follows, let’s
assume that base address is 1048.

Address 1048 1050 1052 1054 1054 1056 1058 1060


Value 1 2 3 4 5 6 7 8
Index a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3]

Now let‟s, discuss platform independent concepts of array element storing


There are two ways to store elements in 2-D array.
1) Row major order
2) Column major order
In row major order elements of array are stored row by row. All the elements of first row
stored and then second row and so on. In example below, array is stored in row major order.

Address 1048 1050 1052 1054 1054 1056 1058 1060


Value 1 2 3 4 5 6 7 8
Index a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3]

In column major order elements of array are stored column by column. All elements of first
column and then, all elements of second column, so on. For the same array, column major
order is shown below.

Address 1048 1050 1052 1054 1054 1056 1058 1060


Value 1 5 2 6 3 7 4 8
Index a[0][0] a[1][0] a[0][1] a[1][1] a[0][2] a[1][2] a[0][3] a[1][3]

By K.P. Kandoriya Page 3


Data Structure: Array

How to calculate address of particular index of 2-D array?


First address of an array is called base address (BA). Address of index in 2-D array depends
on how array is stored? i.e row major order or column major order.

Row major order


In 1-D array,
loc(a[i])=BA(a) + (i-lb)*c
Here a is an array stored in row major order, i is index, loc(a[i]) is address of index i and c is
size of an element and lb is lower bound for i, i.e. first index.
Example: Given a 1-D array a(0:10), stored in row major order with base address 2010, size
of each element is 2 byte. Find the address of a(9).
Here, lb=0, i=9, And c=2, so

Loc(a[9]) =B.A(a)+ (i-lb) * c Simply,

=2010 + (9-0) * 2 1. Multiply i-lb, here it is 9 with c


here it is 2.
=2010 + 9 * 2
=18
=2010 + 18 2. Add it to base address.
2010+18=2028
=2028

In 2-D array,
loc (a[i][j])=BA(a) +{[(i-lb1)*(no of column)+(j-lb2)]*c}

Here a is array stored in row major order, i is row, j is column ([i, j] index), loc(a[i][j]) is
address of index, [ i, j] and c is size of an element, lb1 is lower bound for i, lb2 is lower
bound for j.
No of column = ub2-lb2+1, ub2 is maximum value for j, lb2 is minimum value of j.
Example: Given a 2-D array a(2:6,6:11), stored in row major order with base address 2000,
size of each element is 2 byte. Find the address of a(4,9).

Here, i=4, j=9, And c=2, lb1=2, lb2=6, ub1=6, ub2=9 so


Loc(a[4][9]) = BA(a) +{[(i-lb1)*(no of column)+(j-lb2)]*c}
=2000 +{[(4-2)*(9-6+1)+(9-6)]*2}
=2000 +{[2*4+3]*2}
=2000 +{[8+3]*2}
=2000 +{11*2}

=2000+22 =2022

By K.P. Kandoriya Page 4


Column major order
In 2-D array,

loc(a[i][j])=BA(a) +{[(j-lb2)*(no of row)+(i-lb1)]*c}


Here a is array, i is row, j is column ([i, j] index), loc(a[i][j]) is address of index, [i, j] and c is
size of element, lb1 is lower bound for i and lb2 is lower bound for j.
No of row = ub1-lb1+1, ub1 is maximum value for i.

Example: Find the location/ address of a [2,3] of int array, B.A(a)=2000, lb1=0, lb2=0, ub1=4,
ub2=5.
Here, i=2, j=2, And c=2 (because size of int is 2 byte), so
Loc(a[2][3]) = BA(a) +{[(j-lb2)*(no of row)+(i-lb1)]*c}

=2000 +{[(3-0)*(4-0+1)+(2-0)]*2}
=2000 +{[3*5+2]*2}
=2000 +{[15+2]*2}
=2000 +{17*2}
=2000+34
=2034

Simply,

Draw matrix of size (ub1-lb1+1)*(ub2-lb2+1), Here it is 5*6.

a(0,0) a(0,1) a(0,2) a(0,3) a(0,4) a(0,5)


a(1,0) a(1,1) a(1,2) a(1,3) a(1,4) a(1,5)
a(2,0) a(2,1) a(2,2) a(2,3) a(2,4) a(2,5)
a(3,0) a(3,1) a(3,2) a(3,3) a(3,4) a(3,5)
a(4,0) a(4,1) a(4,2) a(4,3) a(4,4) a(4,5)
Then, count total no of element from first location to given location, in column major order. here
given location is a (2,3) so its 18. Then subtract 1. Multiply it by c. here (18-1)*2=34, add it to base
address, here 2010+30=2034

Operations on 2-d array: all operations for 1-D array are also applicable for 2-d array. In
addition to it 2-d array multiplication can be performed.
Applications of array: There are many application of array, viz. it can be used to represent
list of items. Here we will study one essential application of it that is parse matrix
representation.
By K.P. Kandoriya Page 5
Data Structure: Array and Stack

Sparse Matrix:
Generally 2-D, array used to represent matrix, so for n*m matrix we need at least enough
memory to store all entries that is m*n 2-d array. But what if there are many of entries are
zero, still we need m*n 2-D array.
Thus, if most of entries are zero in any matrix we call it sparse matrix. Simply in sparse
matrix at least m*n/2+1elements are zero. The basic idea to store sparse matrix is to store
only nonzero elements. So we can use different data structure to store sparse matrix. It yields
huge saving in memory requirements for matrix. One of these is array representation of
sparse matrix.
Upper right, lower left, upper left and lower right triangular matrices are sparse matrixes.
Again unit matrix is sparse matrix.

0 1 2 0 0 0 1 2 0 0 0 0 1 0 0
0 0 3 1 0 0 3 0 0 0 0 1 0 1 0
0 0 0 2 3 0 0 0 0 0 2 3 0 0 1
Upper right lower left upper left lower right unit
So any sparse matrix can be represented by array.

Array/single linear list Representation of sparse matrix


We need to scan the nonzero elements of the sparse matrix in row-major order (i.e., scan the
rows left to right beginning with row 1 and picking up the nonzero elements) each nonzero
element is represented by a triple (row, column, value)the list of triples is stored in a 1D
array. It is shown in following example. Let‟s represent 12 in following matrix, it is at 2 nd
row and 1st column so in the list row=2, column=1, and value=12.

0 0 0 List =
12 0 2 Row 2 2 3 3
0 0 0 Column 1 3 2 3
0 23 9 Value 12 2 23 9
Sparse Matrix

Other Applications of the array:


1. Searching Algorithms – Arrays are used in Binary Search and Linear Search.
2. Sorting Algorithms – Algorithms like Bubble Sort, Quick Sort, and Merge Sort work on
arrays.
3. String Handling – Strings are stored and manipulated as arrays of characters.
4. Graph Representation – Arrays are used in adjacency matrices and adjacency lists.
5. Dynamic Programming– Used to store sub-results in problems like Fibonacci, Knapsack, etc

By K.P. Kandoriya Page 6

You might also like