DSA I Week 2 Lecture 1
DSA I Week 2 Lecture 1
Sherajul Arifin
Lecturer, Department of Computer Science and Engineering,
United International University
Data Structure
2
Data Structure
■ Different kinds of data structures are suited to different
kinds of applications, and some are highly specialized to
specific tasks.
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
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]
10
Representation of Array in
Memory
Column Major Order: The array is stored as a sequence of arrays
consisting of columns instead of rows
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
13
Representation of Array in Memory
● Array Mapping Function (AMF): 2D Arrays
Row Major Order:
14
Representation of Array in Memory
● Array Mapping Function (AMF): 2D Arrays
Column Major Order:
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
● 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;
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.
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!