02-Arrays_Mapping_Searching_Linear-Sort
02-Arrays_Mapping_Searching_Linear-Sort
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU 1
Arrays
● An array is an indexed sequence of components
■ The components of an array are all of the same type
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU 2
Representation of Arrays in Memory
● Linear (1 D) Arrays:
A 1-dimensional array a is declared as:
int a[8];
0 1 2 3 4 5 6 7
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU 3
Representation of Arrays in Memory
● 2 D Arrays:
A 2-dimensional array a is declared as:
int a[3][4];
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU 4
Representation of Arrays in Memory
Row Major Order: the array is stored as a sequence of
1-D arrays consisting of rows
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]
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU 5
Representation of Arrays in Memory
Column Major Order: The array is stored as a sequence of
arrays consisting of columns instead of rows
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]
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU 6
Representation of Arrays in Memory: Parameters
● 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
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU 7
Representation of Arrays in Memory
● Array Mapping Function (AMF)
■ AMF converts index value to component address
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU 8
Representation of Arrays in Memory
● Array Mapping Function (AMF): 2D Arrays
Row Major Order:
a : array [l1 .. u1, l2 .. u2] of element_type
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU 9
Representation of Arrays in Memory
● Array Mapping Function (AMF): 2D Arrays
Column Major Order:
a : array [l1 .. u1, l2 .. u2] of element_type
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU 10
Representation of Arrays in Memory
● Array Mapping Function (AMF): 3D Arrays :
a : array [l1 .. u1, l2 .. u2 , l3 .. u3] of element_type
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU 11
Summary on Arrays
● 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.
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU 12
Searching Algorithms:
Linear Search, Binary Search
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU 13
The Searching Problem
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU 14
Linear Search
● 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.
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU 15
Linear Search
/* This program is an example of the 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;
}
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU 16
Linear Search
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU 17
Binary Search
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU 18
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;
}
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU 19
Binary Search
int BinarySearch (int A[], int skey){
int low=0, high=SIZE1, 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;
}
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU 20
Linear-Time Sorting Algorithms:
Counting Sort
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU
Sorting In Linear Time
Counting sort
No comparisons between elements!
But…depends on assumption about the numbers being
sorted
We assume numbers are in the range 1, …, k
The algorithm:
Input: A[1..n], where A[i] {1, 2, 3, …, k}
Output: B[1..n], sorted (notice: not sorting in place)
Also: Array C[1..k] for auxiliary storage
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU
Counting Sort
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU
Counting Sort
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU
Counting Sort
Dr. Md. Abul Kashem Mia, Professor, CSE Dept and Vice Chancellor, UIU