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

1.Linear and Binary Search

Uploaded by

Benin Jacob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

1.Linear and Binary Search

Uploaded by

Benin Jacob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

MODULE 4

TOPIC :SEARCHING
OBJECTIVE

1.To illustrate searching techniques namely


linear and binary search.

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.

linear_search (X, N ,key)


1. Set LOC=0
2. Repeat steps while LOC< N
1. if(X[LOC]==key)
return LOC
else
return -1
End while
Contd.

int x[]= {12, -3, 78, 67, 6, 50, 19,


10};

• Trace the (x,


search following
8, 6)calls
; : Returns 4
search (x, 8, 5) ;

Returns -1

7
#include <stdio.h>

int linear_search (int a[], int size, int key)


{
for (int i=0; i<size; i++)
if (a[i] == key)
return i; return -1;
}

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

– Situation while searching:


• Initially L and R contains the indices of first and last elements.
– Look at the element at index [(L+R)/2].
• Move L or R to the middle depending on the outcome of test.

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;

printf (”\nEnter number to search: ”);


scanf (”%d”, &val);

printf (”\nValue returned: %d \n”,


bin_search (x,8,val);
}

12
Recursive
#include <stdio.h> Version
int bin_search (int a[], int L, int R, int key)
{
int mid;

if (R < L) return -1; /* NOT FOUND


*/ mid = (L + R) / 2;
if (a[mid] < key) return (bin_search(a,mid+1,R,key));
else if (a[mid] > key) return (bin_search(a,L,mid-
1,key));
else return mid; /* FOUND AT INDEX mid */
}

13
int main()
{
int x[]={10,20,30,40,50,60,70,80}, val;

printf (”\nEnter number to search: ”);


scanf (”%d”, &val);

printf (”\nValue returned: %d \n”,


bin_search (x,0,7,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

Access to Not Required Required


middle
element
Worst case O(n) O(log n)
complexity
Complexities of Known Algorithms
Algorithm Best-case Average-case Worst-case
Selection sort O(n )
2 O(n )
2 O(n2)
Insertion sort O(n) O(n2) O(n2)
Bubble sort O(n) O(n2) O(n2)
Quick sort O(n log2n) O(n log2n) O(n2)
Merge sort O(n log2n) O(n log2n) O(n2)
Linear search O(1) O(n) O(n)
Binary search O(1) O(log2n) O(log2n)

78
THANK YOU

You might also like