Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Data Structures and Algorithms (CSO441)

Chapter 3

ARRAY

[Syllabus: Array, Single and multi-dimensional array, Memory


representation (row major and column major) of array, Insertion, and
deletions in array, Advantages and disadvantages of array, Sparse matrices:
Triangular, Tri-diagonal, etc. ]

Instructor
Tanmay De
Department of Computer Science and Engineering
National Institute of Technology, Durgapur
February 02, 2022

Wishing
DATA STRUCTURES AND ALGORITHMSCSO441
you a safe, healthy and happy life
TANMAY DE, CSE, NITD.
Data Structures and Algorithms
CSO441
Text Books:

1. Lipschutz, “Data Structures (Schaum’s Outline Series)”, Tata


Mcgraw Hill.
2. Lipschutz, “Data Structures using C (Schaum’s Outline
Series)”, Tata Mcgraw Hill.
3. E. Horowitz, S. Sahni, S. Anderson-Freed, “Fundamentals of
Data Structures in C”, Universities Press; Second edition
(2008).
4. Deasis samanta, “Classic Data Structures”, PHI, Second edition
(2013).
5. Reema Thareja, “Data Structure using C”, Oxford Press,
Second edition (2014).
6. R. B. Patel, “Data Structure”

Wishing
DATA STRUCTURES AND ALGORITHMS
CSO441
you a safe, healthy and happy life
TANMAY DE, CSE, NITD.
Disclaimer

o The study materials/presentations used in this


course are solely meant for academic purposes
and collected from different available course
materials, slides, books, etc.

o The study materials presented/ distributed can be


reused, reproduced, modified, and distributed by
others for academic purposes only with proper
acknowledgements.

DATA STRUCTURES AND ALGORITHMS CSO441


TANMAY DE, CSE, NITD.
Arrays

 An array is defined as a set of finite, ordered and collection of


homogeneous data elements.
 An array is finite because it contains only a limited number of
elements, and ordered, as all the elements are stored one by
one in contiguous memory locations of the computer memory
in a linear fashion.
 The termed a collection of homogeneous elements means all
the array elements are of the same data type (say integer).

4
DATA STRUCTURES AND ALGORITHMS CSO441
TANMAY DE, CSE, NITD.
Arrays
Terminology
 Size/Length/Dimension

 Type

 Base Address/Base

 Word (w): the size of an element


5
DATA STRUCTURES AND ALGORITHMS CSO441
TANMAY DE, CSE, NITD.
Arrays
Terminology
 Index: All the elements of the array can be referenced by a
subscripts like Ai or A[i], or A(i)

 Range of Indices: Indices of array elements may change


from a lower bound (L) to an upper bound (U), these bounds
are called the boundaries of an array.

6
DATA STRUCTURES AND ALGORITHMS CSO441
TANMAY DE, CSE, NITD.
1-D or Linear Arrays

 One dimensional or Linear array: It is finite collection of n


number of elements of same type such that array element:
o can be referred by name of the array and with the help of
one index.
o the array elements are stored in continuous locations in
the memory.

7
DATA STRUCTURES AND ALGORITHMS CSO441
TANMAY DE, CSE, NITD.
Memory Allocation for Linear Arrays

8
DATA STRUCTURES AND ALGORITHMS CSO441
TANMAY DE, CSE, NITD.
Memory Allocation for Linear Arrays

9
DATA STRUCTURES AND ALGORITHMS CSO441
TANMAY DE, CSE, NITD.
Operations on Linear Arrays

10
DATA STRUCTURES AND ALGORITHMS CSO441
TANMAY DE, CSE, NITD.
Operations on Linear Arrays: Searching
Algorithm Linear Search
//Given a non empty array D with n elements and target element ‘item’. This algorithm
findout the location Loc of the given ‘item’ if present. Otherwise give an appropriate
message for unsuccessful search.

1.K = 1
2.Repeat while (k ≤ n)
3. {
4. if (item == D[k]) then
5. {
6. Display Search is Successful and ‘item’ found at location k
7. Return(k)
8. }
9. k=k+1
10. }
11. Display Search is Unsuccessful
12.Exit
11
DATA STRUCTURES AND ALGORITHMS CSO441
TANMAY DE, CSE, NITD.
Operations on Linear Arrays: Searching
Algorithm Linear Search
// Let, Array D with lower bound is L and upper bound is U.
1.K = L Number of elements (n) = U – L + 1
2.Repeat while (k ≤ U)
3. {
4. if (item == D[k]) then
5. {
6. Display Search is Successful and ‘item’ found at location k
7. Return(k)
8. }
9. k=k+1
10. }
11. Display Search is Unsuccessful
12.Exit

12
DATA STRUCTURES AND ALGORITHMS CSO441
TANMAY DE, CSE, NITD.
Limitation of Big-Ohh

13
DATA STRUCTURES AND ALGORITHMS CSO441
TANMAY DE, CSE, NITD.
Operations on Linear Arrays: Insert

14
DATA STRUCTURES AND ALGORITHMS CSO441
TANMAY DE, CSE, NITD.
Operations on Linear Arrays: Insert

15
DATA STRUCTURES AND ALGORITHMS CSO441
TANMAY DE, CSE, NITD.
Operations on Linear Arrays: Deletion

16
DATA STRUCTURES AND ALGORITHMS CSO441
TANMAY DE, CSE, NITD.
Operations on Linear Arrays: Deletion

17
DATA STRUCTURES AND ALGORITHMS CSO441
TANMAY DE, CSE, NITD.
Operations on Linear Arrays: Merging

18
DATA STRUCTURES AND ALGORITHMS CSO441
TANMAY DE, CSE, NITD.
Operations on Linear Arrays: Sorting (Bubble Sort)

19
DATA STRUCTURES AND ALGORITHMS CSO441
TANMAY DE, CSE, NITD.
Operations on Linear Arrays: Sorting (Bubble Sort)

Algorithm for Bubble Sort


/* */
1. for i = 0 to (n-1)
2. {
3. for j = 0 to (n-i-1)
4. {
5. if (list[j] > list[j+1])
6. Swap(list[j]; list[j+1])
7. }
8. }
20
DATA STRUCTURES AND ALGORITHMS CSO441
9. End of Algorithm
TANMAY DE, CSE, NITD.
Thank You

DATA STRUCTURES AND ALGORITHMS CSO441


TANMAY DE, CSE, NITD.

You might also like