0% found this document useful (0 votes)
41 views23 pages

Binary Search Interpolation Search

Uploaded by

Hayat Hyt
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)
41 views23 pages

Binary Search Interpolation Search

Uploaded by

Hayat Hyt
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/ 23

Binary Search

Interpolation Search
Binary Search
 Definition: Binary search is an efficient algorithm for finding
an element in a sorted array by repeatedly dividing the
search interval in half.

 Key Point: It’s much faster than linear search, but it


requires the array to be sorted.
How Binary Search Works
 Start by checking the middle element of the array.

 If the middle element matches the target, the search is


over.

 If the target is smaller, narrow the search to the left half;


if larger, narrow to the right half.

 Repeat this process until the element is found or the


subarray size becomes zero.
Time Complexity of Binary Search
 Best Case: O(1) - The middle element is the target.

 Worst Case: O(log n) - The array is halved at each


step.

 Average Case: O(log n) - In general, logarithmic


time complexity.
Binary Search vs. Linear Search
 Binary Search: O(log n), much faster but requires
sorted data.

 Linear Search: O(n), checks every element, works


with unsorted data.

 Key Point: Binary search is more efficient for large,


sorted data sets.
Advantages of Binary Search
 Efficiency: Logarithmic time complexity makes binary
search much faster than linear search for large
datasets.

 Fewer Comparisons: Divides the search space in half


with each comparison, leading to fewer overall
comparisons.

 Simple Implementation: Easy to implement and widely


used in practice.
Binary search in java
main method for binary search
Interpolation Search
 Interpolation search is a search algorithm that improves
upon binary search by estimating the position of the target
element based on its value.

 Works efficiently when the data is uniformly distributed.


How Interpolation Search Works
 Calculate the position using the formula based on the
target’s value.

 Check if the value at the estimated position is the target.

 If not, adjust the search range and repeat.

 Continue until the target is found or the range is invalid.


Interpolation Formula
 low + (target - arr[low]) * (high - low) / (arr[high] -
arr[low])

 Explanation:
 This formula estimates the position of the target based on its
value relative to the current range.
Time Complexity
 Best Case: O(1)

 Average case: O(log log n)

 Worst Case: O(n)


Comparison with Binary Search
 Binary Search:
 Divides the search space into halves, regardless of the target’s
value.

 Interpolation Search:
 Estimates the target’s location based on the value, making it
faster for certain data sets.
Interpolation search in java
Questions?

You might also like