Linear and Binary Search
Linear and Binary Search
Linear and Binary Search
1
Description of various
Data Structures : Arrays
2
One dimensional array:
An array with only one row or column is called one-dimensional
array.
It is finite collection of n number of elements of same type such
that:
◦ can be referred by indexing.
◦ The syntax Elements are stored in continuous locations.
◦ Elements x to define one-dimensional array is:
Syntax: Datatype Array_Name [Size];
Where,
Datatype : Type of value it can store (Example: int, char, float)
Array_Name: To identify the array.
Size : The maximum number of elements that the array can hold.
3
Arrays
Simply, declaration of array is as follows:
int arr[10]
Where int specifies the data type or type
of elements arrays stores.
“arr” is the name of array & the number
specified inside the square brackets is the
number of elements an array can store, this is
also called sized or length of array.
4
Represent a Linear Array in memory
The elements of linear array are stored in
consecutive memory locations. It is
shown below:
5
Arrays
◦ The elements of array will always be stored in the
consecutive (continues) memory location.
◦ The number of elements that can be stored in an
array, that is the size of array or its length is given
by the following equation:
(Upperbound-lowerbound)+1
◦ For the above array it would be (9-0)+1=10,where
0 is the lower bound of array and 9 is the upper
bound of array.
◦ Array can always be read or written through loop.
For(i=0;i<=9;i++)
{ scanf(“%d”,&arr[i]);
printf(“%d”,arr[i]); }
6
ADT- ARRAY
structure ARRAY(value, index)
declare
CREATE( ) array
RETRIEVE(array,index) value
STORE(array,index,value) array;
8
Arrays types
Single Dimension
Array
◦ Array with one subscript
A[i]
Two Dimension Array
◦ Array with two
subscripts (Rows and
Column)
◦ A[i][j]
Multi Dimension Array
◦ Array with Multiple 9
Single dimensional array
10
Two dimensional array
A[1][1] 2000
1. Row Major Representation =A[M][N]
A[1][2] 2002
A[3][3] M=3, N=3 A[1][3] 2004
A[2][1] 2006
A[I][J] = BASE ADDRESS+ ((I-1)*N+(J-1)* W)
A[2][2] 2008
A[2][2] = 2000+ (((2-1)*3+(2-1)) * WIDTH) A[2][3] 2010
A[3][1] 2012
= 2000+((3+1)*2) =2008
A[3][2] 2014
A[3][3] 2016
A[3][2] = 2000+ (((3-1)*3+(2-1)) * WIDTH)
= 2000+((6+1)*2) =2014
= 2000+((3+2)*2) =2010
A[1][1] A[1][2] A[1][3]
12
Basic operations of
Arrays
Some common operation
performed on array are:
◦ Traversing
◦ Searching
◦ Insertion
◦ Deletion
◦ Sorting
◦ Merging
13
Traversing Arrays
Traversing: It is used to access each data item exactly once so
that it can be processed.
E.g.
We have linear array A as below:
1 2 3 4 5
10 20 30 40 50
Here we will start from beginning and will go till last element and
during this process we will access value of each element exactly
once as below:
A [1] = 10
A [2] = 20
A [3] = 30
A [4] = 40
A [5] = 50
14
Insertion into Array
Insertion: It is used to add a new data item in the given collection of
data items.
E.g.We have linear array A as below:
1 2 3 4 5
10 20 50 30 15
15
Deletion from Array
Deletion: It is used to delete an existing data item from the given
collection of data items.
16
Searching in
Arrays
Searching: It is used to find out the location of the data item if it exists in the given
collection of data items.
E.g. We have linear array A as below:
1 2 3 4 5
15 50 35 20 25
Suppose item to be searched is 20.We will start from beginning and will compare 20 with each
element. This process will continue until element is found or array is finished. Here:
1) Compare 20 with 15
20 # 15, go to next element.
2) Compare 20 with 50
20 # 50, go to next element.
3) Compare 20 with 35
20 #35, go to next element.
4) Compare 20 with 20
20 = 20, so 20 is found and its location is 4.
17
Linear
10 3 5 7 9 1 66 22 33 71
Search
18
Binary
Search
The binary search
algorithm can be used with
only sorted list of
elements.
Binary Search first divides
a large array into two
smaller sub-arrays and
then recursively operate
the sub-arrays.
Binary Search basically
reduces the search space to
half at each step
19
Binary
Search
20
Procedure binary_search
A ← sorted array 11 22 33 44 55 66
n ← size of array
x ← value to be searched 55 X=55
Set lowerBound = 1 Lb =1
Set upperBound = n =6 Ub = 6
M=1+6/2 =3
While x not found A[M]< 55 33<55 ; lb=4
if upperBound < lowerBound A[M]> 55 33>55
A[M]<=55 33=55
EXIT: x does not exists. Lb=4
Ub=6
set midPoint = lowerBound + upperBound / 2 M=4+6/2 =5
A[M]< 55 55<55
if A[midPoint] < x set lowerBound = midPoint + 1 A[M]> 55 55>55
A[M]<=55 55=55
if A[midPoint] > x set upperBound = midPoint – 1
Ο(n) Ο(log n)
22
Assignment 2
1.
N=10
Array elements 11,22,33,44,55,66,77,88,99,100
Explain the intermediate steps carried out using linear algorithm to find
elements 33, 100, 55, 101(not found condition)
Identify the number of comparisons need for searching the above elements.
2.
N=10
Array elements : 11,22,33,44,55,66,77,88,99,100
i) Explain the intermediate steps carried out using binary algorithm to find
elements each elements as search element.
ii) Identify the number of comparisons need for searching the above elements.
(Assuming each element is search key)
Also indentify the average number of comparisons
iii) Identify the number of comparisons for not found search keys :
5, 40, 80, 111
23