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

DSA I Week 2 Lecture 1

The document provides an overview of data structures, particularly focusing on arrays as a type of linear data structure. It explains the characteristics, advantages, and disadvantages of arrays, as well as the methods for searching elements using linear and binary search algorithms. Additionally, it covers the representation of arrays in memory and the mapping functions for accessing elements efficiently.

Uploaded by

nsalman223675
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

DSA I Week 2 Lecture 1

The document provides an overview of data structures, particularly focusing on arrays as a type of linear data structure. It explains the characteristics, advantages, and disadvantages of arrays, as well as the methods for searching elements using linear and binary search algorithms. Additionally, it covers the representation of arrays in memory and the mapping functions for accessing elements efficiently.

Uploaded by

nsalman223675
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

CSE 2215: Data Structures and Algorithms I

Sherajul Arifin
Lecturer, Department of Computer Science and Engineering,
United International University
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.

2
Data Structure
■ 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.
3
Types of Data Structure
● 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
4
Types of Data Structure
● 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)

5
Arrays
●An array is an indexed sequence of components
■The components of an array are all of the same type
●Typically, the array occupies sequential storage locations
●Array is a static data structure, that is, the length of the array is
determined when the array is created, and cannot be changed

6
Arrays
●Each component of the array has a fixed, unique index
■Indices range from a lower bound to an upper bound
●Any component of the array can be inspected or updated by
using its index
■This is an efficient operation: O(1) = constant time

7
Representation of Array in Memory

● Linear (1 D) Arrays:
A 1-dimensional array a is declared as:
int a[8];
The elements of the array a may be shown as
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]
0 1 2 3 4 5 6 7

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]

8
Representation of Array in
Memory
● 2 D Arrays:
A 2-dimensional array a is declared as:
int a[3][4];
The elements of the array a 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]


In which order are the elements stored?
■Row major order (C, C++, Java support it)
■Column major order (Fortran supports it)
9
Representation of Array in
Memory
Row Major Order: the array is stored as a sequence of 1-D
arrays consisting of rows
a[0][0] a[0][1] a[0][2] a[0] 0
row
[3] row 1
a[1][0] a[1][1] a[1][2] a[1] 2
row
[3]
a[2][0] a[2][1] a[2][2] a[2]
[3]
0 1 2 3 4 5 6 7
8 9 10 11
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]

10
Representation of Array in
Memory
Column Major Order: The array is stored as a sequence of arrays
consisting of columns instead of rows

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]
col 0 col 1 col 2 col 3
0 1 2 3 4 5 6 7
8 9 10 11
a[0][0] a[1][0] a[2][0] a[0][1] a[1][1] a[2][1] a[0][2] a[1][2] a[2][2] a[0][3] a[1][3] a[2][3]

11
Representation of Array in Memory

● 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

12
Representation of Array in Memory
●Array Mapping Function (AMF)
■AMF converts index value to component address

●Linear (1D) Arrays:


a : array [l1 .. u1] of element_type
Then addr(a[i]) = b + (i − l1) × L
= c 0 + c1 × i
Therefore, the time for calculating the address of an element is
same for any value of i.

13
Representation of Array in Memory
● Array Mapping Function (AMF): 2D 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).

14
Representation of Array in Memory
● Array Mapping Function (AMF): 2D 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).

15
Representation of Array in Memory
●Array Mapping Function (AMF): 3D 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).
16
Summary on Array
●Advantages:
■Array is a random access data structure.
■Accessing an element by its index is very fast (constant time)
●Disadvantages:
■Array is a static data structure, that is, the array size is fixed
and can never be changed.
■Insertion into arrays and deletion from arrays are very slow.
●An array is a suitable structure when
■a lot of searching and retrieval are required.
■a small number of insertions and deletions are required.
17
Linear Search
● Each member of the array is visited until the search key is
found.

● Example:
Write a program to search for the search key entered by
the user in the following array:
(9, 4, 5, 1, 7, 78, 22, 15, 96, 45)
You can use the linear search in this example.

18
Linear Search
#include <stdio.h>
#define SIZE 10
int LinearSearch(int [], int); int main() {
int a[SIZE]= {9, 4, 5, 1, 7, 78, 22, 15, 96, 45};
int key, pos;
printf(“Enter the Search Key\n”);
scanf(“%d”, &key);
pos = LinearSearch(a, key);
if(pos == -1)
printf(“The search key is not in the array\n”);
else
printf(“The search key %d is at location %d\n”, key,
pos);
return 0;
} 19
Linear Search
int LinearSearch (int b[ ], int skey) {
int i;

for (i=0; i < SIZE; i++)

if(b[i] == skey)

return i;

return -1;
}

20
Binary Search
● Given a sorted array, Binary Search algorithm can be used to
perform fast searching of a search key on the sorted array.

● The following program implements the binary search algorithm


for the search key entered by the user in the following array:
(3, 5, 9, 11, 15, 17, 22, 25, 37, 68, 72)

21
Binary Search
#include <stdio.h>
#define SIZE 10
int BinarySearch(int [ ], int);
int main(){
int a[SIZE]= {3, 5, 9, 11, 15, 17, 22, 25, 37, 68};
int key, pos;
printf(“Enter the Search Key\n”);
scanf(“%d”,&key);
pos = BinarySearch(a, key);
if(pos == -1)
printf(“The search key is not in the array\n”);
else
printf(“The search key %d is at location %d\n”, key, pos);
return 0;
}
22
Binary Search
int BinarySearch (int A[], int skey){
int low=0, high=SIZE-1, middle;
while(low <= high){
middle = (low+high)/2;
if (skey == A[middle])
return middle;
else if(skey <A[middle])
high = middle - 1;
else
low = middle + 1;
}
return -1;
}

23
Thank you!

You might also like