SlideShare a Scribd company logo
EL Presentation on
Searching Techniques
ALGORITHMS AND DATA STRUCTURES
WITH C++
Submitted by,
Hithesh M P
1RV22EE017
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
Introduction to Searching Techniques
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
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
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;
● }
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
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)
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;
● }
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)
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;
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;
}
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)
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.

More Related Content

PPTX
Binary search
PPTX
Searching and Sorting Algorithms in Data Structures
PPTX
Searching in DSA that follow a dsa searching.pptx
PPTX
Chapter 2 Sorting and Searching .pptx.soft
PPTX
Searching & Algorithms IN DATA STRUCTURES
PDF
Searching, Sorting and Complexity analysis in DS.pdf
PPTX
Linear and binary search
Binary search
Searching and Sorting Algorithms in Data Structures
Searching in DSA that follow a dsa searching.pptx
Chapter 2 Sorting and Searching .pptx.soft
Searching & Algorithms IN DATA STRUCTURES
Searching, Sorting and Complexity analysis in DS.pdf
Linear and binary search

Similar to Data structure and application experential learning project (20)

PPTX
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
PDF
binary search
DOCX
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPTX
abdulla time complexity and linear search.pptx
PDF
DSA Lec 5+6(Search+Sort) (1).pdf
PPTX
Searching_Sorting.pptx
PPT
search_sort.ppt
PPTX
Binary search algorithm.pptx
PPT
Unit iv(dsc++)
PPT
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
PPTX
placement preparation for Array Searching.pptx
PPTX
Dsa – data structure and algorithms searching
DOCX
MODULE 5-Searching and-sorting
PPT
Chapter 14
PDF
Searching and sorting by B kirron Reddi
PPTX
searching in data structure.pptx
PDF
In the binary search, if the array being searched has 32 elements in.pdf
PDF
PPTX
unit II_2_i.pptx
PDF
Searching
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
binary search
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
abdulla time complexity and linear search.pptx
DSA Lec 5+6(Search+Sort) (1).pdf
Searching_Sorting.pptx
search_sort.ppt
Binary search algorithm.pptx
Unit iv(dsc++)
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
placement preparation for Array Searching.pptx
Dsa – data structure and algorithms searching
MODULE 5-Searching and-sorting
Chapter 14
Searching and sorting by B kirron Reddi
searching in data structure.pptx
In the binary search, if the array being searched has 32 elements in.pdf
unit II_2_i.pptx
Searching
Ad

Recently uploaded (20)

PDF
Basic Mud Logging Guide for educational purpose
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
master seminar digital applications in india
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Insiders guide to clinical Medicine.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Basic Mud Logging Guide for educational purpose
Supply Chain Operations Speaking Notes -ICLT Program
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Pre independence Education in Inndia.pdf
O7-L3 Supply Chain Operations - ICLT Program
O5-L3 Freight Transport Ops (International) V1.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
TR - Agricultural Crops Production NC III.pdf
Microbial disease of the cardiovascular and lymphatic systems
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Pharmacology of Heart Failure /Pharmacotherapy of CHF
master seminar digital applications in india
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Sports Quiz easy sports quiz sports quiz
Insiders guide to clinical Medicine.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Ad

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.