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

Linear Search and Binary Search

The document discusses linear and binary search algorithms for efficiently searching through a list of records to find a record with a particular key. Linear search involves sequentially checking each record until the desired key is found or all records are checked, resulting in O(n) time complexity in the worst and average cases. Binary search works on a sorted list by repeatedly dividing the search space in half and only searching the appropriate half, resulting in O(log n) time complexity in both the worst and average cases due to the maximum depth of recursion being log n.

Uploaded by

ADitya
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)
360 views

Linear Search and Binary Search

The document discusses linear and binary search algorithms for efficiently searching through a list of records to find a record with a particular key. Linear search involves sequentially checking each record until the desired key is found or all records are checked, resulting in O(n) time complexity in the worst and average cases. Binary search works on a sorted list by repeatedly dividing the search space in half and only searching the appropriate half, resulting in O(log n) time complexity in both the worst and average cases due to the maximum depth of recursion being log n.

Uploaded by

ADitya
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/ 24

INTRODUCTION

LINEAR SEARCH & BINARY SEARCH


Searching

 We are given a list of records.


 Each record has an associated key.
 Give efficient algorithm for searching for a record containing a
particular key.
 Efficiency is quantified in terms of average time analysis
(number of comparisons) to retrieve an item.

2
Continue…

[0] [1] [2] [3] [4] [ 700 ]


Number 281942902 Number 233667136 Number 580625685
Number 701466868 Number 506643548 Number 155778322

Each record in list has an associated key. Number 580625685


In this example, the keys are ID numbers.

Given a particular key, how can we efficiently


retrieve the record from the list?

3
Linear Search

 Step through array of records, one at a time.


 Look for record with matching key.
 Search stops when
 record with matching key is found
 or when search has examined all records without success.

4
Pseudocode for Linear Search

Algorithm LinearSearch(A,x,n):
Input: Array A of size n, an element x to be searched
Output: Location of searched element.

Set k  1 & loc  0


while loc  0 & k < = n do
If (x = A[k]) then loc  k
else k  k + 1
If loc = 0 then print “Number not found”
else print “loc is the location of item”
Exit

5
Linear Search Analysis

 What are the worst and average case running times for Linear
search?
 We must determine the O-notation for the number of
operations required in search.
 Number of operations depends on n, the number of entries in
the list.

6
Worst Case Time for Linear Search

 For an array of n elements, the worst case


time for Linear search requires n array
accesses: O(n).
 Consider cases where we must loop over all
n records:
 desired record appears in the last position of the array
 desired record does not appear in the array at all

7
Average Case for Linear Search

Assumptions:
1. All keys are equally likely in a search
2. We always search for a key that is in the array
Example:
 We have an array of 10 records.
 If search for the first record, then it requires 1
array access; if the second, then 2 array
accesses. etc.
The average of all these searches is:
(1+2+3+4+5+6+7+8+9+10)/10 = 5.5

8
Average Case Time for Linear Search

Generalize for array size n.

Expression for average-case running time:

(1+2+…+n)/n = n(n+1)/2n = (n+1)/2

Therefore, average case time complexity for Linear


search is O(n).

9
Binary Search

 Perhaps we can do better than O(n) in the average case?


 Assume that we are give an array of records that is
sorted. For instance:
 an array of records with integer keys sorted from
smallest to largest (e.g., ID numbers), or

 an array of records with string keys sorted in


alphabetical order (e.g., names).

10
Binary Search Pseudocode
Algorithm BS(A,low,high,x):
Input: Sorted array A of size n, an element x to be searched
Output: Location of searched element.
If (low=high) then
if (A[low]=x) then return(low) else return(-1)
else
mid  (low + high) /2
If A[mid] = x then print “ found at mid” Return
else If (A[mid] < x) then BS(A,mid+1,high,x)
else BS(A,low,mid-1,x)
Print “x not found”
Exit 11
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

12
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint

13
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is 7 = midpoint key? NO.

14
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is 7 < midpoint key? YES.

15
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Search for the target in the area before midpoint.

16
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint

17
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target = key of midpoint? NO.

18
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target < key of midpoint? NO.

19
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target > key of midpoint? YES.

20
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Search for the target in the area after midpoint.

21
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint.


Is target = midpoint key? YES.
22
Binary Search: Analysis

 Worst case complexity?


 What is the maximum depth of recursive calls in binary search
as function of n?
 Each level in the recursion, we split the array in half (divide by
two).
 Therefore maximum recursion depth is floor(log2n) and worst
case = O(log2n).

 Average case is also = O(log2n).

23
Binary Search Complexity Analysis:
Substitution Method

1 if n = 1
T(n)=
T(n/2)+C if n>1

T(n) = T(n/2) + c
T(n/2)= T(n/4)+c+c
Put the value of T(n/2) in above so T(n)=T(n/4)+c+c . . . . T(n/2^k)+c+c+c.....+c
=T(n/2^k)+c+c....+c up to k
=T(1)+k.c
As we taken n/2^k = 1
K = log n
So Time complexity is O(log n)
24

You might also like