1.Linear and Binary Search
1.Linear and Binary Search
TOPIC :SEARCHING
OBJECTIVE
1
Searchi
ng
• Check if a given element (called key) occurs in
the array.
– Example: array of student records; rollno can be the
key.
• Two methods to be discussed:
– If the array elements are unsorted.
• Linear search
– If the array elements are sorted.
• Binary search
2
Linear Search
3
Basic
• Basic idea:
Concept
– Start at the beginning of the array.
– Inspect elements one by one to see if it matches the key.
• Time complexity:
– A measure of how long an algorithm takes to run.
– If there are n elements in the array:
• Best case: match found in first element (1 search operation)
• Worst case: no match found, or match found in the last element
(n search operations)
• Average case: (n + 1) / 2 search operations
4
• What does the function linear_search do?
– It searches the array for the number to be searched
element by element.
– If a match is found, it returns the array index.
– If not found, it returns -1.
6
Algorithm
• Input: Array with size , N and search element namely key
• Output: Return location of search element if found else return -1
• Data Structure: Array.
Returns -1
7
#include <stdio.h>
int main()
{
int x[]={12,-
3,78,67,6,50,19,10}, key;
printf (”\nEnter number to search: ”);
scanf (”%d”, &key);
printf (”\nValue returned: %d \n”,
linear_search (x,8,key);
}
5
Binary Search
8
Basic
Concept
• Binary search works if the array is sorted.
– Look for the target in the middle.
– If you don’t find it, you can ignore half of the array,
and repeat the process with the other half.
• In every step, we reduce the number of
elements to search by half.
9
The Basic
• What we want?
Strategy
– Find split between values larger and smaller than key:
0 n-1
x: <=key >key
L R
10
Input: Array with size , N and search element namely key
Output: Return location of search element if found else
return -1.
Data Structure: Array.
Binary search (A, N, key)
1.Set left=0, right=N-1
2.Repeat steps 3 and 4 while left<=right
3.mid= (left + right)/ 2
4.If A[mid] < key then
set left=mid+1
else if A[mid] > key
set right=mid-1
else
return mid //found at location-mid
end while
6.Return //not found
•
Iterative
#include <stdio.h> Version
int bin_search (int a[], int size, int key)
{
int L, R, mid;
L = 0; R = size – 1;
while (L
<= R)
{ mid = (L + R) /
2;
if (a[mid] < key) L = mid +
1; else if (a[mid] > key) R =
mid -1;
returnelse- /* NOT FOUND */ mid;
return
} 1; /* FOUND AT
INDEX mid */
}
11
int main()
{
int x[]={10,20,30,40,50,60,70,80}, val;
12
Recursive
#include <stdio.h> Version
int bin_search (int a[], int L, int R, int key)
{
int mid;
13
int main()
{
int x[]={10,20,30,40,50,60,70,80}, val;
14
Time
• Complexity
If there are n elements in the array.
– Number of searches required in the worst case:
• For n log2n (say).
= 64
– Initially, list size = 64.
2k = n, where k is the
– After first compare, list size = 32. number of steps.
– After second compare, list size = 16.
– After third compare, list size = 8……….
k = log2n
– After sixth compare, list size = 1. log264 = 6
log21024 = 10
16
Linear Vs Binary
• Suppose that the array x has 1000 elements.
• Ordinary search
– If key is present in x, it would require 500
comparisons on the average.
• Binary search
– After 1st compare, left with 500 elements.
– After 2nd compare, left with 250 elements.
– After 3rd compare, left with 125 elements.
– After at most 10 steps, you are done.
15
Comparison of linear and binary search
Factors Linear Binary
Search Search
Procedure Compare one Compare
by one middle one
Portion Compared Any one half of
covered after element only array
first
comparison
Sorted Array Not Required Required
78
THANK YOU