0% found this document useful (0 votes)
5 views42 pages

Array Lec1

aRRAY USEFUL TOPICS

Uploaded by

rishabhjangid33
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)
5 views42 pages

Array Lec1

aRRAY USEFUL TOPICS

Uploaded by

rishabhjangid33
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
You are on page 1/ 42

i

lin
Data Structures and Algorithms

a
Sh
ith
Arrays, stacks, queues, linked lists, trees, binary search trees,
binary heaps, graphs.
W
a rn
Le

1
PROGRAMMING CONCEPTS:
 Basics and programming constructs
 Functions

i
lin
 Arrays, Pointers, Structures

a
Sh
ith
DATA STRUCTURES:
 Stacks and Queues W
rn
 Linked Lists
a
Le

 Trees and Graphs

2
Definition: Data Structure is a way to store and organize data so that
it can be used efficiently.

 The data structure name indicates itself that organizing the data in

i
memory.

lin
 There are many ways of organizing the data in the memory as we

a
Sh
have already seen one of the data structures, i.e., array in C
language.

ith
 The data structure is not any programming language like C, C++,
W
java, etc. It is a set of algorithms that we can use in any
rn
programming language to structure the data in the memory.
a

 To structure the data in memory, 'n' number of algorithms were


Le

proposed, and all these algorithms are known as Abstract data types.
These abstract data types are the set of rules.

3
Types of Data Structures
There are two types of data structures:
– Primitive data structure

i
– Non-primitive data structure

a lin
Primitive Data structure

Sh
The primitive data structures are primitive data types. The int, char,
float, double, and pointer are the primitive data structures that can hold

ith
a single value.
Non-Primitive Data structure
W
rn
The non-primitive data structure is divided into two types:
• Linear data structure
a

• Non-linear data structure


Le

4
Linear Data Structure
The arrangement of data in a sequential manner is known as a linear data structure.
The data structures used for this purpose are Arrays, Linked list, Stacks, and
Queues. In these data structures, one element is connected to only one another
element in a linear form.

i
lin
When one element is connected to the 'n' number of elements known as a non-

a
linear data structure. The best example is trees and graphs. In this case, the

Sh
elements are arranged in a random manner.

ith
We will discuss the above data structures in brief in the coming topics. Now, we will
W
see the common operations that we can perform on these data structures.
rn
Data structures can also be classified as:
a
Le

• Static data structure: It is a type of data structure where the size is allocated at
the compile time. Therefore, the maximum size is fixed.

• Dynamic data structure: It is a type of data structure where the size is allocated
at the run time. Therefore, the maximum size is flexible. 5
Major Operations
The major or the common operations that can be performed on the

i
lin
data structures are:

a
• Searching

Sh
ith
• Sorting

• Insertion W
a rn
• Update
Le

• Deletion

6
Advantages of Data structures

The following are the advantages of a data structure:

i
lin
• Efficiency: If the choice of a data structure for implementing a particular
ADT is proper, it makes the program very efficient in terms of time and

a
space.

Sh
• Reusability: The data structures provide reusability means that multiple

ith
client programs can use the data structure.
W
• Abstraction: The data structure specified by an ADT also provides the level
rn
of abstraction. The client cannot see the internal working of the data
a
structure, so it does not have to worry about the implementation part.
Le

The client can only see the interface.

7
Following terminology is used as far as data structures are concerned
• Data: Data can be defined as an elementary value or the collection of values, for
example, student's name and its id are the data about the student.

• Group Items: Data items which have subordinate data items are called Group
item, for example, name of a student can have first name and the last name.

i
a lin
• Record: Record can be defined as the collection of various data items, for

Sh
example, if we talk about the student entity, then its name, address, course and
marks can be grouped together to form the record for the student.

ith
W
• File: A File is a collection of various records of one type of entity.
a rn
• Attribute and Entity: An entity represents the class of certain objects. it contains
Le

various attributes. Each attribute represents the particular property of that entity.

• Field: Field is a single elementary unit of information representing the attribute of


an entity.
8
Need of Data Structures

As applications are getting complexed and amount of data is increasing day by day,
there may arise the following problems:

i
• Processor speed: To handle very large amount of data, high speed processing is

lin
required, but as the data is growing day by day to the billions of files per entity,

a
processor may fail to deal with that much amount of data.

Sh
• Data Search: Consider an inventory size of 106 items in a store, If our application

ith
needs to search for a particular item, it needs to traverse 106 items every time,
W
results in slowing down the search process.
a rn
• Multiple requests: If thousands of users are searching the data simultaneously
Le

on a web server, then there are the chances that a very large server can be failed
during that process.

In order to solve the above problems, data structures are used. Data is organized to
form a data structure in such a way that all items are not required to be searched
9
and required data can be searched instantly.
Le
arn
W
ith
Sh
a lin
10
i
Pointer
• Pointers are the variables that are used to store the location
of value present in the memory. A pointer to a location stores

i
lin
its memory address

a
• The process of obtaining the value stored at a location being

Sh
referenced by a pointer is known as dereferencing.

ith
• It is the same as the index for a textbook where each page is
W
referred by its page number present in the index.
rn
• Such pointers usage helps in the dynamic implementation of
a
various data structures such as stack or list.
Le

11
Pointer Details
• Pointer arithmetic: There are four arithmetic operators that can be used
in pointers: ++, --, +, -

i
lin
• Array of pointers: You can define arrays to hold a number of pointers.

a
• Pointer to pointer: C allows you to have pointer on a pointer and so on.

Sh
ith
• Passing pointers to functions in C: Passing an argument by reference or by
address enable the passed argument to be changed in the calling function
by the called function.
W
rn
• Return pointer from functions in C: C allows a function to return a pointer
a

to the local variable, static variable and dynamically allocated memory as


Le

well.

12
int

int *
Le
arn
W
ith
Sh
a lin
13
i
Syntax:

i
lin
<datatype> *variable_name

a
Sh
Example:
int *ptr1 // ptr1 references to a memory location that holds data of int datatype.

ith
int var = 30;

W
int *ptr1 = &var; // pointer to var
int **ptr2 = & ptr1; // pointer to pointer variable ptr1
a rn
Le

14
Array
Definition
• Arrays are defined as the collection of similar type of data items stored at
contiguous memory locations.

i
lin
• Arrays are the derived data type in C programming language which can

a
store the primitive type of data such as int, char, double, float, etc.

Sh
• Array is the simplest data structure where each data element can be

ith
randomly accessed by using its index number.

W
• For example, if we want to store the marks of a student in 6 subjects, then
rn
we don't need to define different variable for the marks in different
subject. instead of that, we can define an array which can store the marks
a

in each subject at a the contiguous memory locations.


Le

15
Properties of the Array
• Each element is of same data type and carries a same size
i.e. int = 4 bytes.

i
lin
• Elements of the array are stored at contiguous memory
locations where the first element is stored at the smallest

a
memory location.

Sh
ith
• Elements of the array can be randomly accessed since we
W
can calculate the address of each element of the array with
the given base address and the size of data element.
a rn
Le

For example, in C language, the syntax of declaring an array is


like following:

int arr[10]; char arr[10]; float arr[5]


16
Need of using Array
• To store the large amount of data
• Difficult to remember all variable names

i
lin
Example:

a
Sh
Program without array:
#include <stdio.h>

ith
void main ()
{
W
int marks_1 = 56, marks_2 = 78, marks_3 = 88, marks_4 = 76, marks_5 = 56, marks_6 = 89;
a rn
float avg = (marks_1 + marks_2 + marks_3 + marks_4 + marks_5 +marks_6) / 6 ;
Le

printf(avg);
}

17
Program by using array:
#include <stdio.h>
void main ()
{

i
lin
int marks[6] = {56,78,88,76,56,89);
int i;

a
Sh
float avg;
for (i=0; i<6; i++ )

ith
{
avg = avg + marks[i];
} W
rn
printf(avg);
a

}
Le

18
Complexity of Array operations
Time Complexity

i
alin
Sh
ith
Algorithm Average Case Worst Case
Access O(1) W O(1)
rn
Search O(n) O(n)
a

Insertion O(n) O(n)


Le

Deletion O(n) O(n)

Space Complexity
In array, space complexity for worst case is O(n).
19
Advantages of Array
• Array provides the single name for the group of variables of the
same type therefore, it is easy to remember the name of all the
elements of an array.

i
a lin
• Traversing an array is a very simple process, we just need to

Sh
increment the base address of the array in order to visit each

ith
element one by one.

W
• Any element in the array can be directly accessed by using the
rn
index.
a
Le

20
Memory Allocation of the array

All the data elements of an array are stored at contiguous locations in the main
memory.
The name of the array represents the base address or the address of first element

i
in the main memory.

a lin
Each element of the array is represented by a proper indexing.

Sh
The indexing of the array can be defined in three ways.
0 (zero - based indexing) : The first element of the array will be arr[0].

ith
1 (one - based indexing) : The first element of the array will be arr[1].

W
n (n - based indexing) : The first element of the array can reside at any random
index number.
a rn
Le

21
Accessing Elements of an array

To access any random element of an array we need the following information:


• Base Address of the array.

i
lin
• Size of an element in bytes.
• Which type of indexing, array follows.

a
Sh
Address of any element of a 1D array can be calculated by using the following
formula:

ith
W
Byte address of element A[i] = base address + size * ( i - first index)
rn
In an array, A[0…10 ], Base address (BA) = 999, size of an element = 2 bytes, find
a
the location of A[1].
Le

22
2D Array

2D array can be defined as an array of arrays.

i
The 2D array is organized as matrices which can be

lin
represented as the collection of rows and columns.

a
Sh
However, 2D arrays are created to implement a relational
database look alike data structure. It provides ease of holding

ith
bulk of data at once which can be passed to any number of
W
functions wherever required.
a rn
Le

23
How to declare 2D Array

int arr[max_rows][max_columns];

i
a lin
Sh
ith
W
a rn
Le

24
How do we access data in a 2D array :

int x = a[i][j];

i
Example:

lin
a
for ( int i=0; i<n ;i++)

Sh
{
for (int j=0; j<n; j++)

ith
{
a[i][j] = 0;
}
W
rn
}
a
Le

25
Initializing 2D Arrays

When we declare and initialize one dimensional array in C

i
programming simultaneously, we don't need to specify the size

lin
of the array.

a
Sh
However this will not work with 2D arrays. We will have to
define at least the second dimension of the array.

ith
W
The syntax to declare and initialize the 2D array is given as
rn
follows.
a
Le

int arr[2][2] = {{0,1},{2,3}};

26
Mapping 2D array to 1D array :

We do need to map two dimensional array to the one dimensional array in order to store
them in the memory.

i
lin
Example:

a
Sh
ith
W
a rn
Le

There are two main techniques of storing 2D array elements into memory

1. Row Major ordering


2. Column Major ordering

27
Row Major ordering

All the rows of the 2D array are stored into the memory contiguously.

i
Example

a lin
Sh
ith
W
a rn
Le

28
Column Major ordering

All the columns of the 2D array are stored into the memory contiguously.

i
Example

a lin
Sh
ith
W
a rn
Le

29
Calculating the Address of the random element of a 2D array:

Due to the fact that, there are two different techniques of storing the two dimensional array
into the memory, there are two different formulas to calculate the address of a random

i
element of the 2D array.

a lin
By Row Major Order

Sh
Address(A[I][J]) = B+ (I * N + J) *W

ith
or
Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]
W
rn
Where
B = Base address
a

I = Row subscript of element whose address is to be found


Le

J = Column subscript of element whose address is to be found


W = Storage Size of one element stored in the array (in byte)
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)
M = Number of row of the given matrix
N = Number of column of the given matrix 30
Address(A[I][J]) = B+ (I * N + J) *W
or
Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]

i
a lin
Sh
ith
W
a rn
Important : Usually number of rows and columns of a matrix are given ( like A[20][30] or
Le

A[40][60] ) but if it is given as A[Lr- – – – – Ur, Lc- – – – – Uc]. In this case number of rows and
columns are calculated using the following methods:
Number of rows (M) will be calculated as = (Ur – Lr) + 1
Number of columns (N) will be calculated as = (Uc – Lc) + 1
And rest of the process will remain same as per requirement (Row Major Wise or Column
Major Wise). 31
Address(A[I][J]) = B+ (I * N + J) *W
or
Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]

Example:

i
lin
a[10...30, 55...75], base address of the array (BA) = 0, size of an element = 4 bytes .

a
Find the location of a[15][68].

Sh
Address(a[15][68]) = 0 + ((15 - 10) x (75 - 55 + 1) + (68 - 55)) x 4

ith
= (5 x 21 + 13) x 4
= 118 x 4 W
rn
= 472 answer
a
Le

32
By Column major order

Address of A [ I ][ J ] = B+ W*((J*M)+I)

or

i
lin
Address of A [ I ][ J ] = B + W * [( I – Lr ) + M * ( J – Lc )]

a
Sh
Where
B = Base address

ith
I = Row subscript of element whose address is to be found
J = Column subscript of element whose address is to be found

W
W = Storage Size of one element stored in the array (in byte)
Lr = Lower limit of row/start row index of matrix, if not given assume 0 (zero)
rn
Lc = Lower limit of column/start column index of matrix, if not given assume 0 (zero)
a
M = Number of row of the given matrix
Le

N = Number of column of the given matrix

33
Important :
Usually number of rows and columns of a matrix are given ( like A[20][30] or A[40][60] )
but if it is given as A[Lr- – – – – Ur, Lc- – – – – Uc]. In this case number of rows and
columns are calculated using the following methods:
Number of rows (M) will be calculated as = (Ur – Lr) + 1

i
lin
Number of columns (N) will be calculated as = (Uc – Lc) + 1
And rest of the process will remain same as per requirement (Row Major Wise or

a
Sh
Column Major Wise).

ith
W
a rn
Le

34
Address of A [ I ][ J ] = B+ W*((J*M)+I)

or
Address of A [ I ][ J ] = B + W * [( I – Lr ) + M * ( J – Lc )]

i
lin
Important :

a
Sh
Usually number of rows and columns of a matrix are given ( like A[20][30] or A[40][60]
) but if it is given as A[Lr- – – – – Ur, Lc- – – – – Uc]. In this case number of rows and

ith
columns are calculated using the following methods:
Number of rows (M) will be calculated as = (Ur – Lr) + 1

W
Number of columns (N) will be calculated as = (Uc – Lc) + 1
And rest of the process will remain same as per requirement (Row Major Wise or
rn
Column Major Wise).
a
Le

35
Address of A [ I ][ J ] = B+ W*((J*M)+I)

or
Address of A [ I ][ J ] = B + W * [( I – Lr ) + M * ( J – Lc )]

i
a lin
Example:

Sh
A [-5 ... +20][20 ... 70], BA = 1020, Size of element = 8 bytes. Find the location of a[0][30].

ith
Address [A[0][30]) = ((30-20) x (20-(-5)+1) + (0-(-5))) x 8 + 1020
= 265 x 8 + 1020 = 3140 bytes
W
a rn
Le

36
An array X [-15……….10, 15……………40] requires one byte of storage. If beginning location is
1500 determine the location of X [15][20].
Solution:

i
As you see here the number of rows and columns are not given in the question. So they are

lin
calculated as:

a
Number or rows say M = (Ur – Lr) + 1 = [10 – (- 15)] +1 = 26

Sh
Number or columns say N = (Uc – Lc) + 1 = [40 – 15)] +1 = 26

ith
(i) Column Major Wise Calculation of above equation
The given values are: B = 1500, W = 1 byte, I = 15, J = 20, Lr = -15, Lc = 15, M = 26
W
rn
Address of A [ I ][ J ] = B + W * [ ( I – Lr ) + M * ( J – Lc ) ]
= 1500 + 1 * [(15 – (-15)) + 26 * (20 – 15)]
a

= 1500 + 1 * [30 + 26 * 5]
Le

= 1500 + 1 * [160]
= 1660 [Ans]

37
An array X [-15……….10, 15……………40] requires one byte of storage. If beginning location is
1500 determine the location of X [15][20].
Solution:

i
As you see here the number of rows and columns are not given in the question. So they are

lin
calculated as:

a
Number or rows say M = (Ur – Lr) + 1 = [10 – (- 15)] +1 = 26

Sh
Number or columns say N = (Uc – Lc) + 1 = [40 – 15)] +1 = 26

ith
(i) Row Major Wise Calculation of above equation
The given values are: B = 1500, W = 1 byte, I = 15, J = 20, Lr = -15, Lc = 15, N = 26
W
rn
Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]
= 1500 + 1* [26 * (15 – (-15))) + (20 – 15)]
a

= 1500 + 1 * [26 * 30 + 5]
Le

= 1500 + 1 * [780 + 5]
= 1500 + 785
= 2285 [Ans]

38
1. A matrix B[10][20] is stored in the memory with each element requiring 2 bytes of storage.
If the base address at B[2][1] is 2140, find the address of B[5][4] when the matrix is stored in
Column Major Wise.
Address of [I, J]th element in column-major = B + W[R(J – Lc) + (I – Lr)]
= 2140 + 2[10(4 – 1) + (5 – 2)]

i
lin
= 2140 + 2[10 × 3 + 3]
= 2140 + 2[30 + 3]

a
= 2140 + 2[33]

Sh
= 2140 + 66
= 2206

ith
W
2. Each element of an array arr[15][20] requires ‘W’ bytes of storage. If the address of
arr[6][8] is 4440 and the base address at arr[1][1] is 4000, find the width ‘W’ of each cell in
rn
the array arr[][] when the array is stored as Column Major Wise.
a
Address of [I, J]th element in column-major = B + W[R(J – Lc) + (I – Lr)]
Le

⇒ 4440 = 4000 + W[15(8 – 1) + (6 – 1)]


⇒ 4440 = 4000 + W[15(7) + 5]
⇒ 4440 = 4000 + W[105 + 5]
⇒ 4440 = 4000 + W[110]
⇒ W[110] = 440
⇒ W = 4.
39
3. A matrix ARR[-4…6, 3…8] is stored in the memory with each element requiring 4 bytes of
storage. If the base address is 1430, find the address of ARR[3][6] when the matrix is stored
in Row Major Wise.
Number of columns, C = 8 – 3 + 1 = 6.
Address of [I, J]th element in row-major = B + W[C(I – Lr) + (J – Lc)]

i
lin
⇒ Address of ARR[3][6] = 1430 + 4[6(3 – (-4)) + (6 – 3)]
⇒ Address of ARR[3][6] = 1430 + 4[6(3 + 4) + 3]

a
⇒ Address of ARR[3][6] = 1430 + 4[6(7) + 3]

Sh
⇒ Address of ARR[3][6] = 1430 + 4[42 + 3]
⇒ Address of ARR[3][6] = 1430 + 4[45]

ith
⇒ Address of ARR[3][6] = 1430 + 180
⇒ Address of ARR[3][6] = 1610.
W
a rn
Le

40
4. A two-dimensional array defined as X[3…6, -2…2] requires 2 bytes of storage space for
each element. If the array is stored in Row Major Wise order, determine the address of
X[5][1], given the base address as 1200.
Number of columns, C = 2 – (-2) + 1 = 5.
Address of [I, J]th element in row-major = B + W[C(I – Lr) + (J – Lc)]

i
lin
⇒ Address of X[5][1] = 1200 + 2[5(5 – 3) + (1 – (-2))]
⇒ Address of X[5][1] = 1200 + 2[5(2) + (3)]

a
⇒ Address of X[5][1] = 1200 + 2[13]

Sh
⇒ Address of X[5][1] = 1200 + 26
⇒ Address of X[5][1] = 1226.

ith
W
a rn
Le

41
5. Each element of an array A[20][10] requires 2 bytes of storage. If the address of A[6][8] is
4000, find the base address at A[0][0] when the array is stored in Row Major Wise.
Address of [I, J]th element in row-major = B + W[C(I – Lr) + (J – Lc)]
⇒ 4000 = B + 2[10(6 – 0) + (8 – 0)]
⇒ 4000 = B + 2[10(6) + 8]

i
lin
⇒ 4000 = B + 2[60 + 8]
⇒ 4000 = B + 2[68]

a
⇒ 4000 = B + 136

Sh
⇒ B = 3864.

ith
W
a rn
Le

42

You might also like