0% found this document useful (0 votes)
3 views26 pages

1 Array Data Structure-2

The document provides an overview of data structures and algorithms, defining data structures as methods for organizing data and algorithms as sequences of steps for data transformation. It discusses the importance of studying data structures for efficient data management in computer programs and introduces various types of data structures, including primitive and non-primitive, as well as linear and non-linear structures. Additionally, it covers arrays, their types, memory addressing, and their applications in data storage, sorting, and searching.

Uploaded by

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

1 Array Data Structure-2

The document provides an overview of data structures and algorithms, defining data structures as methods for organizing data and algorithms as sequences of steps for data transformation. It discusses the importance of studying data structures for efficient data management in computer programs and introduces various types of data structures, including primitive and non-primitive, as well as linear and non-linear structures. Additionally, it covers arrays, their types, memory addressing, and their applications in data storage, sorting, and searching.

Uploaded by

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

Data Structures and Algorithms

0404201

Chapter 1
Data Structures and
Arrays

1
Introduction
What Are Data Structures and Algorithms?
 A data structure is a method of organizing data in a virtual system.
Think of sequences of numbers, or tables of data: these are both
well-defined data structures.

 A data structure is a specialized format for organizing, processing,


retrieving and storing data.

 There are several basic and advanced types of data structures, all
designed to arrange data to suit a specific purpose.

 Data structures make it easy for users to access and work with the
data they need in appropriate ways.

2
Introduction
What Are Data Structures and Algorithms?
 An algorithm is a sequence of steps executed by a computer
that takes an input and transforms it into a target output.

 Together, data structures and algorithms combine and allow


programmers to build whatever computer programs they’d like.
Deep study into data structures and algorithms ensures well-
optimized and efficient code.

3
Introduction
Why are we studying Data Structures ?

Modern world all about … DATA


……Data needs to be organized and managed

• Computer programs manipulating and processing


data that is stored in a digital memory.
Program = Algorithm + Data Structure
4
Memory and Addressing
 Memory is a storage component in the Computer used to store
application programs.

 The memory of a computer system is divided into chunks or we


can say "sections" which basically hold the converted bits.

 The Memory Chip is divided into equal parts called “CELLS”.

 Each Cell is uniquely identified by a binary number


called “ADDRESS”.

 Computer memory stores the data after converting into bits or


bytes. 5
Memory and Addressing
 For example, the Memory Chip configuration is
represented as ’64 K x 8′ as shown in the figure
below.

6
Memory and Addressing
Memory can be: Byte Addressable Memory OR Word Addressable Memory.

Byte Addressable Memory Word Addressable Memory


Data space in the cell = word length of
Data space in the cell = 8 bits CPU

Bytewise storage Wordwise storage

Data is stored byte by byte. Data is stored word by word.

The necessary condition involves


Suitable for the processes that require computing the address of word that
contains required byte, fetch that word
data comprising single byte at a time. A and then extraction of needed byte from
single address is issued for accessing a the two byte word takes place. So, it is
single byte. indirectly accessible. Hence, modern
machines are byte addressable.

7
Data Structures
• Data Structure: is a way of organizing a collection of data items that considers
not only the elements stored but also their relationship to each other.

• Data Structure: the logical or mathematical model of organization of the data.

• Storage Structure : Representation of a particular data structure in the memory


of a computer.

Stack
Graph
Array 1
2
Linked List head
3 4
7 8
Queue 5 6
F R
8
Data Structures
There are two types of data structure:
a) Primitive data structure (integer, character, etc.).
b) Non-primitive data structure (array, linked list, tree, etc.).

• Primitive vs. Non-Primitive Data Structures

Primitive Data Structure Non-Primitive Data Structure


Predefined by the programming Defined and created by the programmer
languages

The size depends upon the type of data The size of non primitive data structure
structure are not fixed

9
Data Structures
• Linear vs. Non-Linear Data Structure
Linear Data Structure Non-linear Data Structure
Data elements are arranged in a linear Data elements are attached in
order where each and every element is hierarchically manner.
attached to its previous and next
adjacent.
Single level only. Multiple levels are involved.
Easy to implement More complex to implement.
Data elements traversed in a single run Data elements can’t be traversed in a
only. single run only.
Examples: array, stack, queue, linked Examples: trees and graphs.
list, etc.
Applications: Artificial Intelligence and
Applications: software development. image processing.

10
Data Structures
Hierarchy of Data Structures

11
Arrays
• The array is the most important and most general data structure.

• All other data structures can be implemented using an array.

• The computer memory itself is organized as a large, one-


dimensional array.

• Elements of the array are identified (e.g.: can be accessed) by


specifying the name of the array and the element's index

12
Arrays
One-Dimensional Array
– A type of linear array and the elements are represented by a single index which
can either represent a row or column index.

• Indexing:
– Zero-based indexing: The first element of the array is indexed by subscript of 0
– One-based indexing: The first element of the array is indexed by subscript of 1
– n-based indexing: The base index of an array can be freely chosen.

• Operations:
– Create int A[5] = {98, 87, 92, 79, 85};
– Find the size of the array A. length
– Store data elements A[i] = v
– Retrieve data elements A[i]
– Modify data elements A[i] = v
13
Arrays
Two-Dimensional Array
– A type of linear array and the elements are represented by a two indices (row or column
index).
• General array declaration statement:
data-type array-name[number-of-rows][number-of-columns];

• Memory space requirements:


– The number of bytes needed for the array is found by multiplying the size of each
element times the number of elements.

base address Column-address of target element


Row-address of
target element
target element
14
Arrays
The 2-dimensional arrays are stored as 1-dimensional arrays in the computer’s memory,
using one of two ways:
1. Row-Major Implementation:
– The first row elements are placed first, then the second row elements and so on.
– The address of [i, j]th element is: B + W[C(i – Lr) + (j – Lc)]

2. Column-Major Implementation:
– The first column elements are placed first, then the second ….
– The address of [i, j]th element is : B + W[R(j – Lc) + (i – Lr)]

• Notes:
– B is the base address (address of the first block in the array).
– W is the width in bytes (size in bytes for each element).
– Lr is the index of the first row.
– Lc is the index of the first column.
– C is the total number of columns.
– R is the total number of rows. 15
Arrays
• Example (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[0][0] is 2140, find the address of B[5][4] when the
matrix is stored in Column Major Wise, then in Row Major Wise:
– Column-major = B + W[R(j– Lc) + (i – Lr)]
= 2140 + 2[10(4 – 0) + (5 – 0)]
= 2140 + 2[10 × 4 + 5]
= 2140 + 2[40 + 5]
= 2140 + 2[45]
= 2140 + 90
= 2230
– Row-major = B + W[C(i – Lr) + (j – Lc)]
= 2140 + 2[20(5 – 0) + (4 – 0)]
= 2140 + 2[20 × 5 + 4]
= 2140 + 2[100 + 4]
= 2140 + 2[104]
= 2140 + 208
= 2348 16
Arrays
• Example (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 the array arr[][] when the
array is stored as Column Major Wise.

– Address of [i, j]th element in column-major = B + W[R(j – Lc) + (i – Lr)]


⇒ 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. Source: https://www.happycompiler.com/memory-addresses-arrays/

17
Introduction
• Example (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 = B + W[C(i – Lr) + (j – Lc)]
⇒ Address of ARR[3][6] = 1430 + 4[6(3 – (-4)) + (6 – 3)]
⇒ Address of ARR[3][6] = 1430 + 4[6(3 + 4) + 3]
⇒ Address of ARR[3][6] = 1430 + 4[6(7) + 3]
⇒ Address of ARR[3][6] = 1430 + 4[42 + 3]
⇒ Address of ARR[3][6] = 1430 + 4[45]
⇒ Address of ARR[3][6] = 1430 + 180
⇒ Address of ARR[3][6] = 1610.

Source: https://www.happycompiler.com/memory-addresses-arrays/
18
Arrays
• Example (4):

– A matrix A[m][m] is stored in the memory with each element requiring 4


bytes of storage. If the base address at A[1][1] is 1500 and the address of
A[4][5] is 1608, determine the order of the matrix when it is stored in
Column Major Wise.

– Address of [i, j]th element = B + W[R(j – Lc) + (i – Lr)]


⇒ 1608 = 1500 + 4[m(5 – 1) + (4 – 1)]
⇒ 1608 = 1500 + 4[m(4) + 3]
⇒ 1608 = 1500 + 16m + 12
⇒ 1608 = 1512 + 16m
⇒ 16m = 96
⇒ m = 6.
Source: https://www.happycompiler.com/memory-addresses-arrays/
19
Arrays
• Example (5):

– A matrix P[15][10] is stored with each element requiring 8 bytes of


storage. If the base address at P[0][0] is 1400, determine the address
at P[10][7] when the matrix is stored in Row Major Wise.

– Address of [i, j]th element = B + W[C(i – Lr) + (j – Lc)]


⇒ Address at P[10][7] = 1400 + 8[10(10 – 0) + (7 – 0)]
⇒ Address at P[10][7] = 1400 + 8[10(10) + 7]
⇒ Address at P[10][7] = 1400 + 8[100 + 7]
⇒ Address at P[10][7] = 1400 + 8[107]
⇒ Address at P[10][7] = 1400 + 856
⇒ Address at P[10][7] = 2256.
Source: https://www.happycompiler.com/memory-addresses-arrays/
20
Arrays
• Example (6):

– A matrix A[m][n] is stored with each element requiring 4 bytes of


storage. If the base address at A[1][1] is 1500 and the address at
A[4][5] is 1608, determine the number of rows of the matrix
when the matrix is stored in Column Major Wise.

– Address of [i, j]th element = B + W[R(j – Lc) + (i – Lr)]


⇒ 1608 = 1500 + 4[R(5 – 1) + (4 – 1)]
⇒ 1608 = 1500 + 4[4R + 3]
⇒ 1608 = 1500 + 16R + 12
⇒ 1608 = 1512 + 16R
⇒ 16R = 96
⇒ R = 6. Source: https://www.happycompiler.com/memory-addresses-arrays/
21
Arrays
• Example (7):

– The array D[-2…10][3…8] contains double type elements. If the


base address is 4110, find the address of D[4][5], when the
array is stored in Column Major Wise.

– Number of rows, R = 10 – (-2) + 1 = 13.


Address of [I, J]th element = B + W[R(j – Lc) + (i – Lr)]
⇒ Address of D[4][5] = 4110 + 8[13(5 – 3) + (4 – (-2))]
⇒ Address of D[4][5] = 4110 + 8[13(2) + (4 + 2)]
⇒ Address of D[4][5] = 4110 + 8[26 + 6]
⇒ Address of D[4][5] = 4110 + 8[32]
⇒ Address of D[4][5] = 4110 + 256
Source: https://www.happycompiler.com/memory-addresses-arrays/
⇒ Address of D[4][5] = 4366. 22
Arrays
• Example (8):

– An array AR[-4 … 6, -2 … 12], stores elements in Row Major Wise,


with the address AR[2][3] as 4142. If each element requires 2 bytes
of storage, find the Base address.

– Number of columns, C = 12 – (-2) + 1 = 12 + 2 + 1 = 15.


Address of [I, J]th element = B + W[C(i – Lr) + (j – Lc)]
⇒ 4142 = B + 2[15(2 – (-4)) + (3 – (-2))]
⇒ 4142 = B + 2[15(2 + 4) + (3 + 2)]
⇒ 4142 = B + 2[15(6) + 5]
⇒ 4142 = B + 2[90 + 5]
⇒ 4142 = B + 2[95]
⇒ 4142 = B + 190 Source: https://www.happycompiler.com/memory-addresses-arrays/

⇒ B = 3952. 23
Applications of Arrays
• Storing and accessing data in a specific order: For example, to store the
scores of a group of students, or the temperatures recorded by a weather
station.
• Sorting data in ascending or descending order: Sorting algorithms such as
bubble sort, merge sort, and quicksort rely heavily on arrays.
• Searching for specific elements: using algorithms such as linear search and
binary search.
• To represent matrices: in mathematical computations such as matrix
multiplication, linear algebra, and image processing.
• For implementing stacks and queues.
• For representing graphs: Each element in the array represents a node in the
graph, and the relationships between the nodes are represented by the
values stored in the array.

24
Advantages of Arrays
• Efficient access to elements: Arrays provide direct and efficient access to any
element in the collection. Time required to access an element is constant and
does not depend on the size of the array.
• Fast data retrieval: The data can be accessed quickly and efficiently without
the need for complex data structures or algorithms.
• Memory efficiency: The elements of an array are stored in contiguous
memory locations, the size of the array is known at compile time. This means
that memory can be allocated for the entire array in one block, reducing
memory fragmentation.
• Versatility: Arrays can be used to store a wide range of data types, including
integers, floating-point numbers, characters, and even complex data
structures such as objects and pointers.
• Easy to implement and understood
• Compatible with hardware
25
Disadvantages of Arrays
• Fixed size: Arrays have a fixed size that is determined at the time of creation. This
means that if the size of the array needs to be increased, a new array must be
created and the data must be copied from the old array to the new array, which
can be time-consuming and memory-intensive.
• Memory allocation issues: If the size of the array is too large, the system may run
out of memory, which can cause the program to crash.
• Insertion and deletion issues: All the elements after the insertion or deletion point
must be shifted to accommodate the change which is inefficient and time-
consuming.
• Wasted space: If an array is not fully populated, there can be wasted space in the
memory allocated for the array.
• Limited data type support: Arrays have limited support for complex data types
such as objects and structures, as the elements of an array must all be of the same
data type.
• Lack of flexibility: The fixed size and limited support for complex data types can
make arrays inflexible compared to other data structures such as linked lists and
trees. 26

You might also like