Search Algorithms
Sequential Search (Linear Search)
Binary Search
Data Structures Using C++ 1
Sequential Search
O (n)
• A sequential search of a list/array begins at the beginning of the
list/array and continues until the item is found or the entire
list/array has been searched
Data Structures Using C++ 2
Sequential Search
bool LinSearch(double x[ ], int n, double item){
for(int i=0;i<n;i++){
if(x[i]==item) return true;
else return false;
}
return false;
}
Data Structures Using C++ 3
Search Algorithms
Data Structures Using C++ 4
Suppose that there are n elements in the array. The following expression
gives the average number of comparisons:
It is known that
Therefore, the following expression gives the average number of comparisons
made by the sequential search in the successful case:
Search Algorithms
Data Structures Using C++ 5
Binary Search
O(log2 n)
•A binary search looks for an
item in a list using a divide-and-
conquer strategy
Data Structures Using C++ 6
Binary Search
• Binary search algorithm assumes that the items in the
array being searched are sorted
• The algorithm begins at the middle of the array in a
binary search
• If the item for which we are searching is less than the
item in the middle, we know that the item won’t be in
the second half of the array
• Once again we examine the “middle” element
• The process continues with each comparison cutting in
half the portion of the array where the item might be
Data Structures Using C++ 7
Binary Search
Data Structures Using C++ 8
Binary Search: middle element
Data Structures Using C++ 9
left + right
2
mid =
Binary Search
bool BinSearch(double list[ ], int n, double item, int&index){
int left=0;
int right=n-1;
int mid;
while(left<=right){
mid=(left+right)/2;
if(item> list [mid]){ left=mid+1; }
else if(item< list [mid]){right=mid-1;}
else{
item= list [mid];
index=mid;
return true; }
}// while
return false;
}
Data Structures Using C++ 10
Binary Search: Example
Data Structures Using C++ 11
Binary Search
Data Structures Using C++ 12
Binary Search
Data Structures Using C++ 13
Binary Search Tree
Data Structures Using C++ 14
Binary Search Tree
O(log2 n)
bool search(Node * root, int id) {
bool found = false;
if(root == NULL) return false;
if(root->data == id) {
cout<<" The node is found "<<endl;
found = true;
}
if(!found) found = search(root->left, id);
if(!found) found = search(root->right, id);
return found;
}
Data Structures Using C++ 15
Binary Search Tree
Data Structures Using C++ 16
Binary Search Tree
Data Structures Using C++ 17
Binary Search Tree
Data Structures Using C++ 18

More Related Content

PPT
Searching algorithms
PPT
Searching algorithms
PPTX
21CS32 DS Module 1 PPT.pptx
PDF
searching
PPTX
Searching and Sorting Algorithms in Data Structures
PDF
Searching
PPTX
Chapter 2 Sorting and Searching .pptx.soft
PPSX
Algorithm and Programming (Searching)
Searching algorithms
Searching algorithms
21CS32 DS Module 1 PPT.pptx
searching
Searching and Sorting Algorithms in Data Structures
Searching
Chapter 2 Sorting and Searching .pptx.soft
Algorithm and Programming (Searching)

Similar to 06-search-ar121r11111ay-1-LinearBinary.ppt (20)

PPTX
Searching in DSA that follow a dsa searching.pptx
PDF
UNIT IV -Data Structures.pdf
PPT
9780324782011_PPT_ch09.ppt
PPT
Chapter 4: basic search algorithms data structure
PPTX
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
PPTX
Chapter 3 - Data Structure and Algorithms.pptx
PDF
PPTX
Lecture07 the linked-list_as_a_data_structure_v3
PPTX
Sequential & binary, linear search
DOCX
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPTX
Searching_Sorting.pptx
PPT
Chap10
DOCX
UNIT V.docx
PPT
PPT
Arrays in C++
PPTX
Searching techniques
DOCX
MODULE 5-Searching and-sorting
PPTX
Lecture3b searching
PPTX
Searching & Algorithms IN DATA STRUCTURES
Searching in DSA that follow a dsa searching.pptx
UNIT IV -Data Structures.pdf
9780324782011_PPT_ch09.ppt
Chapter 4: basic search algorithms data structure
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
Chapter 3 - Data Structure and Algorithms.pptx
Lecture07 the linked-list_as_a_data_structure_v3
Sequential & binary, linear search
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Searching_Sorting.pptx
Chap10
UNIT V.docx
Arrays in C++
Searching techniques
MODULE 5-Searching and-sorting
Lecture3b searching
Searching & Algorithms IN DATA STRUCTURES
Ad

Recently uploaded (20)

PPTX
_Dispute Resolution_July 2022.pptxmhhghhhh
PPTX
D1basicstoloopscppforbeginnersgodoit.pptx
PPTX
430838499-Anaesthesiiiia-Equipmenooot.pptx
PDF
Beginner’s Guide to Digital Marketing.pdf
PDF
Sheri Ann Lowe Compliance Strategist Resume
PDF
servsafecomprehensive-ppt-full-140617222538-phpapp01.pdf
PPTX
mathsportfoliomanvi-211121071838 (1).pptx
PPTX
Unit 2 CORPORATE CULTURE AND EXPECTATIONS
PPTX
E-commerce Security and Fraud Issues and Protection
PDF
LSR CASEBOOK 2024-25.pdf. very nice casbook
PPTX
GPAT Presentation PPT and details about imp topics.pptx
PPTX
ANN DL UNIT 1 ANIL 13.10.24.pptxcccccccccc
PPTX
STS CHAP 4 human development as reflected
PPTX
Unit 3 Presentation Etiquette Business and Corporate Etiquette
PPT
notes_Lecture2 23l3j2 dfjl dfdlkj d 2.ppt
PDF
iTop VPN Crack Latest Version 2025 Free Download With Keygen
DOCX
PRACTICE-TEST-12 is specially designed for those
PPTX
ChandigarhUniversityinformationcareer.pptx
PDF
Basic GMP - Training good manufacturing procedure
PPT
ppt-of-extraction-of-metals-12th-1.pptb9
_Dispute Resolution_July 2022.pptxmhhghhhh
D1basicstoloopscppforbeginnersgodoit.pptx
430838499-Anaesthesiiiia-Equipmenooot.pptx
Beginner’s Guide to Digital Marketing.pdf
Sheri Ann Lowe Compliance Strategist Resume
servsafecomprehensive-ppt-full-140617222538-phpapp01.pdf
mathsportfoliomanvi-211121071838 (1).pptx
Unit 2 CORPORATE CULTURE AND EXPECTATIONS
E-commerce Security and Fraud Issues and Protection
LSR CASEBOOK 2024-25.pdf. very nice casbook
GPAT Presentation PPT and details about imp topics.pptx
ANN DL UNIT 1 ANIL 13.10.24.pptxcccccccccc
STS CHAP 4 human development as reflected
Unit 3 Presentation Etiquette Business and Corporate Etiquette
notes_Lecture2 23l3j2 dfjl dfdlkj d 2.ppt
iTop VPN Crack Latest Version 2025 Free Download With Keygen
PRACTICE-TEST-12 is specially designed for those
ChandigarhUniversityinformationcareer.pptx
Basic GMP - Training good manufacturing procedure
ppt-of-extraction-of-metals-12th-1.pptb9
Ad

06-search-ar121r11111ay-1-LinearBinary.ppt

  • 1. Search Algorithms Sequential Search (Linear Search) Binary Search Data Structures Using C++ 1
  • 2. Sequential Search O (n) • A sequential search of a list/array begins at the beginning of the list/array and continues until the item is found or the entire list/array has been searched Data Structures Using C++ 2
  • 3. Sequential Search bool LinSearch(double x[ ], int n, double item){ for(int i=0;i<n;i++){ if(x[i]==item) return true; else return false; } return false; } Data Structures Using C++ 3
  • 4. Search Algorithms Data Structures Using C++ 4 Suppose that there are n elements in the array. The following expression gives the average number of comparisons: It is known that Therefore, the following expression gives the average number of comparisons made by the sequential search in the successful case:
  • 6. Binary Search O(log2 n) •A binary search looks for an item in a list using a divide-and- conquer strategy Data Structures Using C++ 6
  • 7. Binary Search • Binary search algorithm assumes that the items in the array being searched are sorted • The algorithm begins at the middle of the array in a binary search • If the item for which we are searching is less than the item in the middle, we know that the item won’t be in the second half of the array • Once again we examine the “middle” element • The process continues with each comparison cutting in half the portion of the array where the item might be Data Structures Using C++ 7
  • 9. Binary Search: middle element Data Structures Using C++ 9 left + right 2 mid =
  • 10. Binary Search bool BinSearch(double list[ ], int n, double item, int&index){ int left=0; int right=n-1; int mid; while(left<=right){ mid=(left+right)/2; if(item> list [mid]){ left=mid+1; } else if(item< list [mid]){right=mid-1;} else{ item= list [mid]; index=mid; return true; } }// while return false; } Data Structures Using C++ 10
  • 11. Binary Search: Example Data Structures Using C++ 11
  • 14. Binary Search Tree Data Structures Using C++ 14
  • 15. Binary Search Tree O(log2 n) bool search(Node * root, int id) { bool found = false; if(root == NULL) return false; if(root->data == id) { cout<<" The node is found "<<endl; found = true; } if(!found) found = search(root->left, id); if(!found) found = search(root->right, id); return found; } Data Structures Using C++ 15
  • 16. Binary Search Tree Data Structures Using C++ 16
  • 17. Binary Search Tree Data Structures Using C++ 17
  • 18. Binary Search Tree Data Structures Using C++ 18