Data structure and application experential learning project
1. EL Presentation on
Searching Techniques
ALGORITHMS AND DATA STRUCTURES
WITH C++
Submitted by,
Hithesh M P
1RV22EE017
2. Introduction to Searching Techniques
● Searching is a fundamental operation in Data Structures and Algorithms. It
involves finding the position of a target value within a data structure.
● Types:
● 1. Linear Search
● 2. Binary Search
● 3. Interpolation Search
4. Linear Search
The simplest way to find an element in a list is to check if it matches the
sought after value
• Best case: O(1) (if found at the start)
• Worst case: O(n) (if found at the end or not present)
Worst case: the entire list must be linearly searched
This occurs when the value is in the last position or not found
The average case requires searching half of the list
The best case occurs when the value is in the first element in the list
5. Linear Search
Linear Search Algorithm:
for all elements in the list do
if element == value_to_find then return position_of(element)
end # if
end # for
Consider using this search on a list that has duplicate elements
You cannot assume that once one element is found, the search is done
Thus, you need to continue searching through the entire list
6. Linear Search
● #include <iostream>
● using namespace std;
● int linearSearch(int arr[], int n, int x) {
● for (int i = 0; i < n; i++) {
● if (arr[i] == x) return i;
● }
● return -1;
● }
● int main() {
● int arr[] = {10, 20, 30, 40, 50};
● int x = 30;
● int index = linearSearch(arr, 5, x);
● if (index != -1) cout << "Element found at index " << index;
● else cout << "Element not found";
● return 0;
● }
7. Binary Search
For binary search, begin searching at the middle of the list
If the item is less than the middle, check the middle item between the first
item and the middle
If it is more than the middle item, check the middle item of the section
between the middle and the last section
The process stops when the value is found or when the
remaining list of elements to search consists of one value
Following this process reduces half the search space
The algorithm is an O(log2(n))
Equivalent to O(log(n))
This is the same for the average and worst cases
8. Binary Search
Keep in mind that a binary search requires an ordered list
An unsorted list needs to be sorted before the search
If the search occurs rarely, you should not sort the list
If the list is updated infrequently, sort and then search the list
Check values immediately preceding and following the current position to
modify the search to work with duplicates
Best case: O(1) (if found in the middle)
Worst case: O(log n) (recursive divisions)
9. Binary Search
● #include <iostream>
● using namespace std;
● int binarySearch(int arr[], int left, int right, int x) {
● while (left <= right) {
● int mid = left + (right - left) / 2;
● if (arr[mid] == x) return mid;
● if (arr[mid] < x) left = mid + 1;
● else right = mid - 1;
● }
● return -1;
● }
● int main() {
● int arr[] = {10, 20, 30, 40, 50};
● int x = 30;
● int index = binarySearch(arr, 0, 4, x);
● if (index != -1) cout << "Element found at index " << index;
● else cout << "Element not found";
● return 0;
● }
10. Interpolation Search
● Interpolation Search is an improved variant of Binary Search that works
efficiently when elements in a sorted array are uniformly distributed. Instead
of always checking the middle element (as in Binary Search), Interpolation
Search estimates the position of the target value using a formula.
● • Works like Binary Search but estimates the position using interpolation.
● • Best case: O(1)
● • Worst case: O(n) (when values are non-uniformly distributed)
11. Interpolation Search
● #include <iostream>
● using namespace std;
● int interpolationSearch(int arr[], int n, int x) {
● int low = 0, high = n - 1;
● while (low <= high && x >= arr[low] && x <= arr[high]) {
● // Estimate the position of x
● int pos = low + ((x - arr[low]) * (high - low) / (arr[high] - arr[low]));
● // If the element is found
● if (arr[pos] == x)
● return pos;
● // If x is larger, search in the right subarray
● if (arr[pos] < x)
● low = pos + 1;
12. Interpolation Search
// If x is smaller, search in the left subarray
else
high = pos - 1;
}
return -1; // Element not found
}
int main() {
int arr[] = {10, 20, 30, 40, 50, 60, 70, 80, 90};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 50;
int index = interpolationSearch(arr, n, x);
if (index != -1)
cout << "Element found at index " << index << endl;
else
cout << "Element not found" << endl;
return 0;
}
13. Comparison of Searching Techniques
• Linear Search: Works on unsorted arrays, but slow.
• Binary Search: Works on sorted arrays, faster than linear.
• Interpolation Search: Best for uniformly distributed sorted
data.
Complexities:
Search Type Best Case Worst Case
Linear Search O(1) O(n)
Binary Search O(1) O(log n)
Interpolation Search O(1) O(n)
14. Conclusion
● Searching is crucial in DSA, used in various applications.
● Choose the right technique based on data type and structure.
● Binary Search and Interpolation Search are optimal for sorted arrays.