SlideShare a Scribd company logo
1 of 12
Lecture by:
Ms Surbhi Saroha
Centre for Distance and Online Education
Shobhit Institute of Engineering & Technology
[ NAAC “A” Grade Accredited, Deemed to be University ]
Shobhit Institute of Engineering and Technology (NAAC 'A'
Grade Deemed to be University)
Syllabus
• Searching techniques,
• linear search
• binary search
Shobhit Institute of Engineering and Technology (NAAC 'A'
Grade Deemed to be University)
Searching techniques
• Searching is the process of finding a particular item in a collection of items.
• A search typically answers whether the item is present in the collection or
not.
• Searching requires a key field such as name, ID, code which is related to the
target item.
• When the key field of a target item is found, a pointer to the target item is
returned.
• The pointer may be an address, an index into a vector or array, or some
other indication of where to find the target.
• If a matching key field isn’t found, the user is informed.
Shobhit Institute of Engineering and Technology (NAAC 'A'
Grade Deemed to be University)
linear search
• Linear Search Algorithm is the simplest search algorithm.
• In this search algorithm a sequential search is made over all the items
one by one to search for the targeted item.
• Each item is checked in sequence until the match is found.
• If the match is found, particular item is returned otherwise the search
continues till the end.
Shobhit Institute of Engineering and Technology (NAAC 'A'
Grade Deemed to be University)
Algorithm
• LinearSearch ( Array A, Value x)
• Step 1: Set i to 1
• Step 2: if i > n then go to step 7
• Step 3: if A[i] = x then go to step 6
• Step 4: Set i to i + 1
• Step 5: Go to Step 2
• Step 6: Print Element x Found at index i and go to step 8
• Step 7: Print element not found
• Step 8: Exit
Shobhit Institute of Engineering and Technology (NAAC 'A'
Grade Deemed to be University)
Linear Search complexity
Shobhit Institute of Engineering and Technology (NAAC 'A'
Grade Deemed to be University)
Case Time Complexity
Best Case O(1)
Average Case O(n)
Worst Case O(n)
Binary Search Algorithm
• Binary Search Algorithm is fast according to run time complexity.
• This algorithm works on the basis of divide and conquer rule.
• In this algorithm we have to sort the data collection in ascending order first
then search for the targeted item by comparing the middle most item of
the collection.
• If match found, the index of item is returned. If the middle item is greater
than the targeted item, the item is searched in the sub-array to the left of
the middle item.
• Otherwise, the item is searched for in the sub-array to the right of the
middle item.
• This process continues on the sub-array as well until the size of the sub
array reduces to zero.
Shobhit Institute of Engineering and Technology (NAAC 'A'
Grade Deemed to be University)
Algorithm
• Step 1: Data list must be ordered list in ascending order.
• Step 2: Probe middle of list
• Step 3: If target equals list[mid], FOUND.
• Step 4: If target < list[mid], discard 1/2 of list between list[mid] and list[last].
• Step 5: If target > list[mid], discard 1/2 of list between list[first] and list[mid].
• Step 6: Continue searching the shortened list until either the target is found, or there are
no elements to probe.
Shobhit Institute of Engineering and Technology (NAAC 'A'
Grade Deemed to be University)
Complexity Analysis of Binary Search:
• Time Complexity:
• Best Case: O(1)
• Average Case: O(log N)
• Worst Case: O(log N)
Shobhit Institute of Engineering and Technology (NAAC 'A'
Grade Deemed to be University)
Advantages of Binary Search:
• Binary search is faster than linear search, especially for large arrays.
• More efficient than other searching algorithms with a similar time
complexity, such as interpolation search or exponential search.
• Binary search is well-suited for searching large datasets that are
stored in external memory, such as on a hard drive or in the cloud.
Shobhit Institute of Engineering and Technology (NAAC 'A'
Grade Deemed to be University)
Drawbacks of Binary Search:
• The array should be sorted.
• Binary search requires that the data structure being searched be
stored in contiguous memory locations.
• Binary search requires that the elements of the array be comparable,
meaning that they must be able to be ordered.
Shobhit Institute of Engineering and Technology (NAAC 'A'
Grade Deemed to be University)
THANK YOU
Shobhit Institute of Engineering and Technology (NAAC 'A'
Grade Deemed to be University)

More Related Content

Similar to Searching in Data Structure(Linear search and Binary search)

Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operationsbabuk110
 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfMustafaJutt4
 
searching techniques.pptx
searching techniques.pptxsearching techniques.pptx
searching techniques.pptxDr.Shweta
 
Searching Algorithms for students of CS and IT using C++
Searching Algorithms for students of CS and IT using C++Searching Algorithms for students of CS and IT using C++
Searching Algorithms for students of CS and IT using C++shahidameer8
 
programming in C
programming in Cprogramming in C
programming in CADITHYAM19
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm03446940736
 
Searching, Sorting and Hashing Techniques
Searching, Sorting and Hashing TechniquesSearching, Sorting and Hashing Techniques
Searching, Sorting and Hashing TechniquesSelvaraj Seerangan
 
ADSA orientation.pptx
ADSA orientation.pptxADSA orientation.pptx
ADSA orientation.pptxKiran Babar
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary searchZia Ush Shamszaman
 
Alogorithm ppr slideshow
Alogorithm ppr slideshowAlogorithm ppr slideshow
Alogorithm ppr slideshowKartikAgarwal82
 
unit II_2_i.pptx
unit II_2_i.pptxunit II_2_i.pptx
unit II_2_i.pptxHODElex
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)jehan1987
 

Similar to Searching in Data Structure(Linear search and Binary search) (20)

Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdf
 
searching techniques.pptx
searching techniques.pptxsearching techniques.pptx
searching techniques.pptx
 
Searching Algorithms for students of CS and IT using C++
Searching Algorithms for students of CS and IT using C++Searching Algorithms for students of CS and IT using C++
Searching Algorithms for students of CS and IT using C++
 
programming in C
programming in Cprogramming in C
programming in C
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
 
Searching, Sorting and Hashing Techniques
Searching, Sorting and Hashing TechniquesSearching, Sorting and Hashing Techniques
Searching, Sorting and Hashing Techniques
 
ADSA orientation.pptx
ADSA orientation.pptxADSA orientation.pptx
ADSA orientation.pptx
 
Amitdsppt
AmitdspptAmitdsppt
Amitdsppt
 
Quick Sort By Prof Lili Saghafi
Quick Sort By Prof Lili SaghafiQuick Sort By Prof Lili Saghafi
Quick Sort By Prof Lili Saghafi
 
cs702 ppt.ppt
cs702 ppt.pptcs702 ppt.ppt
cs702 ppt.ppt
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
Binary search
Binary search Binary search
Binary search
 
Alogorithm ppr slideshow
Alogorithm ppr slideshowAlogorithm ppr slideshow
Alogorithm ppr slideshow
 
unit II_2_i.pptx
unit II_2_i.pptxunit II_2_i.pptx
unit II_2_i.pptx
 
SearchAlgorithm.pdf
SearchAlgorithm.pdfSearchAlgorithm.pdf
SearchAlgorithm.pdf
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
 
Data Structures 8
Data Structures 8Data Structures 8
Data Structures 8
 

More from SURBHI SAROHA

Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2SURBHI SAROHA
 
Management Information System(Unit 2).pptx
Management Information System(Unit 2).pptxManagement Information System(Unit 2).pptx
Management Information System(Unit 2).pptxSURBHI SAROHA
 
Management Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxManagement Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxSURBHI SAROHA
 
Introduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptxIntroduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptxSURBHI SAROHA
 
Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)SURBHI SAROHA
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)SURBHI SAROHA
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1SURBHI SAROHA
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)SURBHI SAROHA
 
OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)SURBHI SAROHA
 

More from SURBHI SAROHA (20)

Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2
 
Management Information System(Unit 2).pptx
Management Information System(Unit 2).pptxManagement Information System(Unit 2).pptx
Management Information System(Unit 2).pptx
 
Management Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxManagement Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptx
 
Introduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptxIntroduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptx
 
JAVA (UNIT 5)
JAVA (UNIT 5)JAVA (UNIT 5)
JAVA (UNIT 5)
 
DBMS (UNIT 5)
DBMS (UNIT 5)DBMS (UNIT 5)
DBMS (UNIT 5)
 
DBMS UNIT 4
DBMS UNIT 4DBMS UNIT 4
DBMS UNIT 4
 
JAVA(UNIT 4)
JAVA(UNIT 4)JAVA(UNIT 4)
JAVA(UNIT 4)
 
OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)
 
OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)
 
DBMS UNIT 3
DBMS UNIT 3DBMS UNIT 3
DBMS UNIT 3
 
JAVA (UNIT 3)
JAVA (UNIT 3)JAVA (UNIT 3)
JAVA (UNIT 3)
 
Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)
 
DBMS (UNIT 2)
DBMS (UNIT 2)DBMS (UNIT 2)
DBMS (UNIT 2)
 
JAVA UNIT 2
JAVA UNIT 2JAVA UNIT 2
JAVA UNIT 2
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)
 

Recently uploaded

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 

Recently uploaded (20)

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 

Searching in Data Structure(Linear search and Binary search)

  • 1. Lecture by: Ms Surbhi Saroha Centre for Distance and Online Education Shobhit Institute of Engineering & Technology [ NAAC “A” Grade Accredited, Deemed to be University ] Shobhit Institute of Engineering and Technology (NAAC 'A' Grade Deemed to be University)
  • 2. Syllabus • Searching techniques, • linear search • binary search Shobhit Institute of Engineering and Technology (NAAC 'A' Grade Deemed to be University)
  • 3. Searching techniques • Searching is the process of finding a particular item in a collection of items. • A search typically answers whether the item is present in the collection or not. • Searching requires a key field such as name, ID, code which is related to the target item. • When the key field of a target item is found, a pointer to the target item is returned. • The pointer may be an address, an index into a vector or array, or some other indication of where to find the target. • If a matching key field isn’t found, the user is informed. Shobhit Institute of Engineering and Technology (NAAC 'A' Grade Deemed to be University)
  • 4. linear search • Linear Search Algorithm is the simplest search algorithm. • In this search algorithm a sequential search is made over all the items one by one to search for the targeted item. • Each item is checked in sequence until the match is found. • If the match is found, particular item is returned otherwise the search continues till the end. Shobhit Institute of Engineering and Technology (NAAC 'A' Grade Deemed to be University)
  • 5. Algorithm • LinearSearch ( Array A, Value x) • Step 1: Set i to 1 • Step 2: if i > n then go to step 7 • Step 3: if A[i] = x then go to step 6 • Step 4: Set i to i + 1 • Step 5: Go to Step 2 • Step 6: Print Element x Found at index i and go to step 8 • Step 7: Print element not found • Step 8: Exit Shobhit Institute of Engineering and Technology (NAAC 'A' Grade Deemed to be University)
  • 6. Linear Search complexity Shobhit Institute of Engineering and Technology (NAAC 'A' Grade Deemed to be University) Case Time Complexity Best Case O(1) Average Case O(n) Worst Case O(n)
  • 7. Binary Search Algorithm • Binary Search Algorithm is fast according to run time complexity. • This algorithm works on the basis of divide and conquer rule. • In this algorithm we have to sort the data collection in ascending order first then search for the targeted item by comparing the middle most item of the collection. • If match found, the index of item is returned. If the middle item is greater than the targeted item, the item is searched in the sub-array to the left of the middle item. • Otherwise, the item is searched for in the sub-array to the right of the middle item. • This process continues on the sub-array as well until the size of the sub array reduces to zero. Shobhit Institute of Engineering and Technology (NAAC 'A' Grade Deemed to be University)
  • 8. Algorithm • Step 1: Data list must be ordered list in ascending order. • Step 2: Probe middle of list • Step 3: If target equals list[mid], FOUND. • Step 4: If target < list[mid], discard 1/2 of list between list[mid] and list[last]. • Step 5: If target > list[mid], discard 1/2 of list between list[first] and list[mid]. • Step 6: Continue searching the shortened list until either the target is found, or there are no elements to probe. Shobhit Institute of Engineering and Technology (NAAC 'A' Grade Deemed to be University)
  • 9. Complexity Analysis of Binary Search: • Time Complexity: • Best Case: O(1) • Average Case: O(log N) • Worst Case: O(log N) Shobhit Institute of Engineering and Technology (NAAC 'A' Grade Deemed to be University)
  • 10. Advantages of Binary Search: • Binary search is faster than linear search, especially for large arrays. • More efficient than other searching algorithms with a similar time complexity, such as interpolation search or exponential search. • Binary search is well-suited for searching large datasets that are stored in external memory, such as on a hard drive or in the cloud. Shobhit Institute of Engineering and Technology (NAAC 'A' Grade Deemed to be University)
  • 11. Drawbacks of Binary Search: • The array should be sorted. • Binary search requires that the data structure being searched be stored in contiguous memory locations. • Binary search requires that the elements of the array be comparable, meaning that they must be able to be ordered. Shobhit Institute of Engineering and Technology (NAAC 'A' Grade Deemed to be University)
  • 12. THANK YOU Shobhit Institute of Engineering and Technology (NAAC 'A' Grade Deemed to be University)