0% found this document useful (0 votes)
11 views25 pages

Searching

The document discusses different search algorithms for efficiently finding a record with a particular key in a list of records. It describes serial search, which has O(n) worst-case and average-case time complexity, and binary search, which has O(log n) worst-case and average-case time complexity when the list is sorted. Binary search works by repeatedly dividing the search space in half and checking the key at the midpoint, eliminating one half based on whether the target key is lower or higher than the midpoint key.

Uploaded by

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

Searching

The document discusses different search algorithms for efficiently finding a record with a particular key in a list of records. It describes serial search, which has O(n) worst-case and average-case time complexity, and binary search, which has O(log n) worst-case and average-case time complexity when the list is sorted. Binary search works by repeatedly dividing the search space in half and checking the key at the midpoint, eliminating one half based on whether the target key is lower or higher than the midpoint key.

Uploaded by

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

Data Structures (KCS-301)

Searching
Lecture W3.L1

Department of Computer Science and


Engineering
Problem: Search

• 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.
Search

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

Number 701466868
Number 281942902 Number 233667136 Number 580625685 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?
Serial 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.
Pseudocode for Serial Search
// Search for a desired item in the n array elements
// starting at a[first].
// Returns pointer to desired record if found.
// Otherwise, return NULL

for(i = 1 to n)
{
if(a[i] == item)
return i;
}
If(i==n+1)
Print: “Not Found”;
Serial Search Analysis

• What are the worst and average case running times for serial
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.
Worst Case Time for Serial Search

• If item is present at the last or item is not present at all.

• For an array of n elements, the worst case time for serial


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
Average Case for Serial 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
Average Case Time for Serial 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 serial search is


O(n).
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).
Binary Search Pseudocode
if(size == 0)
found = false;
else {
middle = index of approximate midpoint of array segment;
if(target == a[middle])
target has been found!
else if(target < a[middle])
search for target in area before midpoint;
else
search for target in area after midpoint;
}
Binary Search

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

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

3 6 7 11 32 33 53
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


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.


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.


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.


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


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.


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.


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.


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.


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.
Pseudo Code - Iterative
Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched
lowerBound = 1
upperBound = n
Do
{
Mid = (lowerBound+upperBound)/2;
if(A[Mid] = = x)
break;
else if (x < A[Mid])
upperBound = Mid – 1;
else
lowerBound = Mid + 1;

}While(lowerBound < = upperBound);

if(lowerBound < = upperBound)


Print: “Element Found at Mid”;
else
Print: “Not Found”;
Pseudo Code - Recursive
Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched
lowerBound = 1
upperBound = n

BinarySearch(A[],lowerBound,upperBound,ITEM)
{
if(if(lowerBound > upperBound)
return -1;

Mid = (lowerBound+upperBound)/2;

if(A[Mid] = = x)
return Mid;
else if (x < A[Mid])
BinarySearch(A[], lowerBound, Mid-1, ITEM);
else
BinarySearch(A[], Mid+1, upperBound, ITEM);

}
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).

You might also like