0% found this document useful (0 votes)
17 views

01-Introduction Arrays

This document introduces the topic of data structures. It discusses that data structures provide efficient ways to store and organize data in a computer. It notes two main types of data structures - linear and nonlinear. Linear data structures like arrays and linked lists are discussed in more detail. The properties, parameters and memory representation of arrays are explained, including for multi-dimensional arrays stored in row-major and column-major order.

Uploaded by

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

01-Introduction Arrays

This document introduces the topic of data structures. It discusses that data structures provide efficient ways to store and organize data in a computer. It notes two main types of data structures - linear and nonlinear. Linear data structures like arrays and linked lists are discussed in more detail. The properties, parameters and memory representation of arrays are explained, including for multi-dimensional arrays stored in row-major and column-major order.

Uploaded by

sabbir hossain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Data Structures

Introduction

Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 1
The Course

● Text Books
■ Introduction to Algorithms (Third edition)
Cormen, Leiserson, Rivest, and Stein
○ An excellent reference you should own

■ Data Structures and Algorithms in C++


Goodrich, Tamassia, and Mount

● Instructor: Dr. Md. Abul Kashem Mia


■ Professor, CSE Dept, BUET
[email protected]

Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 2
What is a Data Structure?
● Data is a collection of facts, such as values, numbers, words,
measurements, or observations.
● Structure means a set of rules that holds the data together.

● A data structure is a particular way of storing and organizing


data in a computer so that it can be used efficiently.
■ Different kinds of data structures are suited to different kinds of
applications, and some are highly specialized to specific tasks.
■ Data structures provide a means to manage huge amount of data
efficiently.
■ Usually, efficient data structures are a key to designing efficient
algorithms.
■ Data structures can be nested.

Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 3
Types of Data Structures
● Data structures are classified as either
■ Linear (e.g, arrays, linked lists), or
■ Nonlinear (e.g, trees, graphs, etc.)

● A data structure is said to be linear if it satisfies the following four


conditions
■ There is a unique element called the first
■ There is a unique element called the last
■ Every element, except the last, has a unique successor
■ Every element, except the first, has a unique predecessor

● There are two ways of representing a linear data structure in memory


■ By means of sequential memory locations (arrays)
■ By means of pointers or links (linked lists)

Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 4
Arrays

● Data is often available in tabular form


● Tabular data is often represented in arrays
● Matrix is an example of tabular data and is often
represented as a 2-dimensional array
■ Matrices are normally indexed beginning at 1 rather than 0

Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 5
Properties of Arrays

● The components of an array are all of the same type.

● Array is a random access data structure.

● Array is a static data structure.

● Access time for an array element is constant, that is, O(1).

● An array is a suitable structure when a small number of


insertions and deletions are required.
● An array is a suitable structure when a lot of searching and
retrieval are required.

Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 6
Parameters of Arrays

● Base Address (b): The memory address of the first byte of the
first array component.
● Component Length (L): The memory required to store one
component of an array.
● Upper and Lower Bounds (li, ui): Each index type has a
smallest value and a largest value.
● Dimension

Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 7
Representation of Arrays in Memory
● Array Mapping Function (AMF)
■ AMF converts index value to component address

● Linear (1 D) Arrays:
a : array [l1 .. u1] of element_type
Then addr(a[i]) = b + (i  l1)  L
= c0 + c1  i
Therefore, the time for calculating the address of an element is same
for any value of i.

Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 8
Representation of Arrays in Memory
● Array Mapping Function (AMF)
■ AMF converts index value to component address

● 2 D Arrays:
a : array [l1 .. u1, l2 .. u2] of element_type

In which order are the elements stored?


■ Row major order (C, C++, Java support it)
■ Column major order (Fortran supports it)

Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 9
Representation of Arrays in Memory

● 2 D Arrays:
The elements of a 2-dimensional array a are declared as:
int a[3][4];

may be shown as a table


a[0][0] a[0][1] a[0][2] a[0][3]
a[1][0] a[1][1] a[1][2] a[1][3]
a[2][0] a[2][1] a[2][2] a[2][3]

Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 10
Representation of Arrays in Memory

Row Major Order:

a[0][0] a[0][1] a[0][2] a[0][3] row 0


a[1][0] a[1][1] a[1][2] a[1][3] row 1
a[2][0] a[2][1] a[2][2] a[2][3] row 2

Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 11
Representation of Arrays in Memory

Column Major Order:

a[0][0] a[0][1] a[0][2] a[0][3]


a[1][0] a[1][1] a[1][2] a[1][3]
a[2][0] a[2][1] a[2][2] a[2][3]

column 0 column 1 column 2 column 3

Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 12
Representation of Arrays in Memory
● Array Mapping Function (AMF)
■ AMF converts Index value to component address

● 2 D Arrays (Row Major Order):


a : array [l1 .. u1, l2 .. u2] of element_type

Then addr(a[i, j]) = b + (i  l1)(u2  l2 + 1)L + (j  l2)L


= c0 + c1i + c2j

Therefore, the time for calculating the address of an element is


same for any value of (i, j).

Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 13
Representation of Arrays in Memory
● Array Mapping Function (AMF)
■ AMF converts Index value to component address

● 2 D Arrays (Column Major Order):


a : array [l1 .. u1, l2 .. u2] of element_type

Then addr(a[i, j]) = b + (j  l2)(u1  l1 + 1)L + (i  l1)L


= c0 + c1i + c2j

Therefore, the time for calculating the address of an element is


same for any value of (i, j).

Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 14
Representation of Arrays in Memory
● Array Mapping Function (AMF)
■ AMF converts Index value to component address

● 3 D Arrays :
a : array [l1 .. u1, l2 .. u2 , l3 .. u3] of element_type

Then addr(a[i, j, k]) = b + (i  l1)(u2  l2 + 1)(u3  l3 + 1)L +


(j  l2)(u3  l3 + 1)L + (k  l3)L
= c0 + c1i + c2j + c3k

Therefore, the time for calculating the address of an element is


same for any value of (i, j , k).

Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 15

You might also like