SlideShare a Scribd company logo
BINARY
SEARCH
Prepared by :
Dimpy (1833)
Drishti (1838)
INTODUCTION
A Binary search algorithm finds the position of a specified input value
(the search "key") within a sorted array . For binary search, the array
should be arranged in ascending or descending order.
Why Binary Search Is Better
Than Linear Search ?
A linear search works by looking at each element in a list of data
until it either finds the target or reaches the end. This results in
O(n) performance on a given list. A binary search comes with the
prerequisite that the data must be sorted. We can use this
information to decrease the number of items we need to look at to
find our target. That is ,
Binary Search is fast as compared to linear search because of
less number of computational operations .
Example :
Step 1 :
Step 2:
Step 3:
ALGORITHM (Recursive)
recursivebinarysearch(int A[], int first, int last, int key)
if last<first
index=-1
else
int mid = (first + last) / 2
if key=A[mid]
index=mid
else if key<A[mid]
index= recursivebinarysearch(A, first, mid – 1, key)
else
index= recursivebinarysearch(A, mid + 1, last, key)
return index
BINARY SEARCH ALGORITHM
Algorithm is quite simple. It can be done either recursively or
iteratively:
 Get the middle element;
 If the middle element equals to the searched value, the algorithm
stops;
 Otherwise, two cases are possible:
 Searched value is less, than the middle element. In this case, go to
the step 1 for the part of the array, before middle element.
 Searched value is greater, than the middle element. In this case, go
to the step 1 for the part of the array, after middle element.
 Now, The iterations should stop when searched element is found &
when sub array has no elements. In this case, we can conclude,
that searched value is not present in the array.
ANALYSIS OF BINARY SEARCH
To analyze the binary search algorithm, we need to recall that each
comparison eliminates about half of the remaining items from
consideration. If we start with n items, about n/2 items will be left
after the first comparison. After the second comparison, there will
be about n/4. Then n/8, n/16, and so on.
When we split the list enough times, we end up with a list that has
just one item. Either that is the item we are looking for or it is not.
Either way, we are done. The number of comparisons necessary to
get to this point is i where n/2^i=1. Solving for i gives us i=(log n).
The maximum number of comparisons is log n. Therefore, the
complexity of binary search is O(log n).
CORRECTNESS OF BINARY SEARCH
 Lemma: For all n>=0, if BinarySearch(E, first , last, k) is called, and
the size is (last-first+1)=n and E[first], …E[last] are in ascending order,
then it returns -1 if k doesn’t occur in E within the range first, … last
and it returns index such that k=E[index] otherwise
PROOF BY INDUCTION
Base case:
n = last − first + 1 = 0
 The array is empty, so first = last + 1
 Line 1 i.e. (first > last) is true , line 2 is reached and the algorithm
correctly returns -1.
 Therefore, the element is not present in the array.
Inductive step: For n>0, assume that the binary search(E, first,
last ,k) satisfies the lemma on size k such that 0<=k<n, and first and
the last are any indexes such that k=last-first+1
 For n>0, the algorithm first calculates mid = (first +
last) /2 thus line 1 is false, as mid is within the
search range (first<=mid<=last)
 If line 5 is true, the procedure terminates with
index=mid
 If line 5 is false, from (first<=mid<=last) and def of n,
(mid -1)- first +1<=(n -1) and last –(mid+1)+1<=(n-1)
 If line 7 is true, then line 8 is executed ,we can
assure that the call accomplishes the objective of
binary search. If line 8 returns a positive index then
our procedure is finished, and if it returns -1, this
implies that k is not present in E in the range first,….
mid-1. Also since line 7 is true , k is not in E in range
mid,... last , so returning -1 is correct.
 If line 7 is false, then line 10 is executed and the
argument is similar.
THANK YOU !

More Related Content

What's hot

Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
Janki Shah
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
Shubham Dwivedi
 
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
03446940736
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithm
maamir farooq
 
Binary search Algorithm
Binary search AlgorithmBinary search Algorithm
Binary search Algorithm
FazalRehman79
 
Linear Search Presentation
Linear Search PresentationLinear Search Presentation
Linear Search Presentation
Markajul Hasnain Alif
 
Queue ppt
Queue pptQueue ppt
Queue ppt
SouravKumar328
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
Mohamed Loey
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
anooppjoseph
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
Mohamed Loey
 
Searching
SearchingSearching
Searching
Ashim Lamichhane
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
almaqboli
 
Hashing
HashingHashing
Hashing
Amar Jukuntla
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its typesNavtar Sidhu Brar
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data Structure
Talha Shaikh
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Pranay Neema
 
Insertion sort
Insertion sortInsertion sort
Insertion sortMYER301
 
Sorting
SortingSorting
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
District Administration
 

What's hot (20)

Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
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
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithm
 
Binary search Algorithm
Binary search AlgorithmBinary search Algorithm
Binary search Algorithm
 
Linear Search Presentation
Linear Search PresentationLinear Search Presentation
Linear Search Presentation
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
Searching
SearchingSearching
Searching
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Hashing
HashingHashing
Hashing
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data Structure
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Sorting
SortingSorting
Sorting
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
 

Similar to Binary Search - Design & Analysis of Algorithms

Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
sajinis3
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
poongothai11
 
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
ASMAALWADEE2
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
SylrizcinMarieManzo3
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
ThenmozhiK5
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Binary Search Algorithm.pptx
Binary Search Algorithm.pptxBinary Search Algorithm.pptx
Binary Search Algorithm.pptx
BhagyashreeMadan1
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
nikshaikh786
 
SEARCHING
SEARCHINGSEARCHING
SEARCHING
SWATHIR72
 
Linear Search
Linear SearchLinear Search
Linear Search
SWATHIR72
 
21-algorithms.ppt
21-algorithms.ppt21-algorithms.ppt
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
ParagAhir1
 
21-algorithms.ppt
21-algorithms.ppt21-algorithms.ppt
21-algorithms.ppt
ashwinraiyani1
 
Algorithm, Pseudocode and Flowcharting in C++
Algorithm, Pseudocode and Flowcharting in C++Algorithm, Pseudocode and Flowcharting in C++
Algorithm, Pseudocode and Flowcharting in C++
Johnny Jean Tigas
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sortingguest2cb109
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
infanciaj
 
L1803016468
L1803016468L1803016468
L1803016468
IOSR Journals
 
21-algorithms (1).ppt
21-algorithms (1).ppt21-algorithms (1).ppt
21-algorithms (1).ppt
DaniloMislosAlbay
 

Similar to Binary Search - Design & Analysis of Algorithms (20)

Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
 
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
Binary Search Algorithm.pptx
Binary Search Algorithm.pptxBinary Search Algorithm.pptx
Binary Search Algorithm.pptx
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
 
SEARCHING
SEARCHINGSEARCHING
SEARCHING
 
Linear Search
Linear SearchLinear Search
Linear Search
 
21-algorithms.ppt
21-algorithms.ppt21-algorithms.ppt
21-algorithms.ppt
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
21-algorithms.ppt
21-algorithms.ppt21-algorithms.ppt
21-algorithms.ppt
 
Algorithm, Pseudocode and Flowcharting in C++
Algorithm, Pseudocode and Flowcharting in C++Algorithm, Pseudocode and Flowcharting in C++
Algorithm, Pseudocode and Flowcharting in C++
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
L1803016468
L1803016468L1803016468
L1803016468
 
21-algorithms (1).ppt
21-algorithms (1).ppt21-algorithms (1).ppt
21-algorithms (1).ppt
 

More from Drishti Bhalla

Propositions - Discrete Structures
Propositions - Discrete Structures Propositions - Discrete Structures
Propositions - Discrete Structures
Drishti Bhalla
 
Physical Layer Numericals - Data Communication & Networking
Physical Layer  Numericals - Data Communication & NetworkingPhysical Layer  Numericals - Data Communication & Networking
Physical Layer Numericals - Data Communication & Networking
Drishti Bhalla
 
Determinants - Mathematics
Determinants - MathematicsDeterminants - Mathematics
Determinants - Mathematics
Drishti Bhalla
 
Matrices - Mathematics
Matrices - MathematicsMatrices - Mathematics
Matrices - Mathematics
Drishti Bhalla
 
Holy Rivers - Hindi
Holy Rivers - HindiHoly Rivers - Hindi
Holy Rivers - Hindi
Drishti Bhalla
 
Mid point line Algorithm - Computer Graphics
Mid point line Algorithm - Computer GraphicsMid point line Algorithm - Computer Graphics
Mid point line Algorithm - Computer Graphics
Drishti Bhalla
 
Unix Memory Management - Operating Systems
Unix Memory Management - Operating SystemsUnix Memory Management - Operating Systems
Unix Memory Management - Operating Systems
Drishti Bhalla
 
Collections Api - Java
Collections Api - JavaCollections Api - Java
Collections Api - Java
Drishti Bhalla
 
Airline Reservation System - Software Engineering
Airline Reservation System - Software EngineeringAirline Reservation System - Software Engineering
Airline Reservation System - Software Engineering
Drishti Bhalla
 
Performance Management and Feedback - SHRM
Performance Management and Feedback - SHRMPerformance Management and Feedback - SHRM
Performance Management and Feedback - SHRM
Drishti Bhalla
 
Computer System Architecture - BUN instruction
Computer System Architecture - BUN instructionComputer System Architecture - BUN instruction
Computer System Architecture - BUN instruction
Drishti Bhalla
 
King of acids -Sulphuric Acid H2SO4
King of acids -Sulphuric Acid H2SO4King of acids -Sulphuric Acid H2SO4
King of acids -Sulphuric Acid H2SO4
Drishti Bhalla
 
Information Technology - System Threats
Information Technology - System ThreatsInformation Technology - System Threats
Information Technology - System Threats
Drishti Bhalla
 
Software Metrics - Software Engineering
Software Metrics - Software EngineeringSoftware Metrics - Software Engineering
Software Metrics - Software Engineering
Drishti Bhalla
 
CNF & Leftmost Derivation - Theory of Computation
CNF & Leftmost Derivation - Theory of ComputationCNF & Leftmost Derivation - Theory of Computation
CNF & Leftmost Derivation - Theory of Computation
Drishti Bhalla
 
Fd & Normalization - Database Management System
Fd & Normalization - Database Management SystemFd & Normalization - Database Management System
Fd & Normalization - Database Management System
Drishti Bhalla
 

More from Drishti Bhalla (16)

Propositions - Discrete Structures
Propositions - Discrete Structures Propositions - Discrete Structures
Propositions - Discrete Structures
 
Physical Layer Numericals - Data Communication & Networking
Physical Layer  Numericals - Data Communication & NetworkingPhysical Layer  Numericals - Data Communication & Networking
Physical Layer Numericals - Data Communication & Networking
 
Determinants - Mathematics
Determinants - MathematicsDeterminants - Mathematics
Determinants - Mathematics
 
Matrices - Mathematics
Matrices - MathematicsMatrices - Mathematics
Matrices - Mathematics
 
Holy Rivers - Hindi
Holy Rivers - HindiHoly Rivers - Hindi
Holy Rivers - Hindi
 
Mid point line Algorithm - Computer Graphics
Mid point line Algorithm - Computer GraphicsMid point line Algorithm - Computer Graphics
Mid point line Algorithm - Computer Graphics
 
Unix Memory Management - Operating Systems
Unix Memory Management - Operating SystemsUnix Memory Management - Operating Systems
Unix Memory Management - Operating Systems
 
Collections Api - Java
Collections Api - JavaCollections Api - Java
Collections Api - Java
 
Airline Reservation System - Software Engineering
Airline Reservation System - Software EngineeringAirline Reservation System - Software Engineering
Airline Reservation System - Software Engineering
 
Performance Management and Feedback - SHRM
Performance Management and Feedback - SHRMPerformance Management and Feedback - SHRM
Performance Management and Feedback - SHRM
 
Computer System Architecture - BUN instruction
Computer System Architecture - BUN instructionComputer System Architecture - BUN instruction
Computer System Architecture - BUN instruction
 
King of acids -Sulphuric Acid H2SO4
King of acids -Sulphuric Acid H2SO4King of acids -Sulphuric Acid H2SO4
King of acids -Sulphuric Acid H2SO4
 
Information Technology - System Threats
Information Technology - System ThreatsInformation Technology - System Threats
Information Technology - System Threats
 
Software Metrics - Software Engineering
Software Metrics - Software EngineeringSoftware Metrics - Software Engineering
Software Metrics - Software Engineering
 
CNF & Leftmost Derivation - Theory of Computation
CNF & Leftmost Derivation - Theory of ComputationCNF & Leftmost Derivation - Theory of Computation
CNF & Leftmost Derivation - Theory of Computation
 
Fd & Normalization - Database Management System
Fd & Normalization - Database Management SystemFd & Normalization - Database Management System
Fd & Normalization - Database Management System
 

Recently uploaded

block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
DuvanRamosGarzon1
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
MuhammadTufail242431
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 

Recently uploaded (20)

block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 

Binary Search - Design & Analysis of Algorithms

  • 1. BINARY SEARCH Prepared by : Dimpy (1833) Drishti (1838)
  • 2. INTODUCTION A Binary search algorithm finds the position of a specified input value (the search "key") within a sorted array . For binary search, the array should be arranged in ascending or descending order. Why Binary Search Is Better Than Linear Search ? A linear search works by looking at each element in a list of data until it either finds the target or reaches the end. This results in O(n) performance on a given list. A binary search comes with the prerequisite that the data must be sorted. We can use this information to decrease the number of items we need to look at to find our target. That is , Binary Search is fast as compared to linear search because of less number of computational operations .
  • 7. ALGORITHM (Recursive) recursivebinarysearch(int A[], int first, int last, int key) if last<first index=-1 else int mid = (first + last) / 2 if key=A[mid] index=mid else if key<A[mid] index= recursivebinarysearch(A, first, mid – 1, key) else index= recursivebinarysearch(A, mid + 1, last, key) return index
  • 8. BINARY SEARCH ALGORITHM Algorithm is quite simple. It can be done either recursively or iteratively:  Get the middle element;  If the middle element equals to the searched value, the algorithm stops;  Otherwise, two cases are possible:  Searched value is less, than the middle element. In this case, go to the step 1 for the part of the array, before middle element.  Searched value is greater, than the middle element. In this case, go to the step 1 for the part of the array, after middle element.  Now, The iterations should stop when searched element is found & when sub array has no elements. In this case, we can conclude, that searched value is not present in the array.
  • 9. ANALYSIS OF BINARY SEARCH To analyze the binary search algorithm, we need to recall that each comparison eliminates about half of the remaining items from consideration. If we start with n items, about n/2 items will be left after the first comparison. After the second comparison, there will be about n/4. Then n/8, n/16, and so on. When we split the list enough times, we end up with a list that has just one item. Either that is the item we are looking for or it is not. Either way, we are done. The number of comparisons necessary to get to this point is i where n/2^i=1. Solving for i gives us i=(log n). The maximum number of comparisons is log n. Therefore, the complexity of binary search is O(log n).
  • 10. CORRECTNESS OF BINARY SEARCH  Lemma: For all n>=0, if BinarySearch(E, first , last, k) is called, and the size is (last-first+1)=n and E[first], …E[last] are in ascending order, then it returns -1 if k doesn’t occur in E within the range first, … last and it returns index such that k=E[index] otherwise PROOF BY INDUCTION Base case: n = last − first + 1 = 0  The array is empty, so first = last + 1  Line 1 i.e. (first > last) is true , line 2 is reached and the algorithm correctly returns -1.  Therefore, the element is not present in the array. Inductive step: For n>0, assume that the binary search(E, first, last ,k) satisfies the lemma on size k such that 0<=k<n, and first and the last are any indexes such that k=last-first+1
  • 11.  For n>0, the algorithm first calculates mid = (first + last) /2 thus line 1 is false, as mid is within the search range (first<=mid<=last)  If line 5 is true, the procedure terminates with index=mid  If line 5 is false, from (first<=mid<=last) and def of n, (mid -1)- first +1<=(n -1) and last –(mid+1)+1<=(n-1)  If line 7 is true, then line 8 is executed ,we can assure that the call accomplishes the objective of binary search. If line 8 returns a positive index then our procedure is finished, and if it returns -1, this implies that k is not present in E in the range first,…. mid-1. Also since line 7 is true , k is not in E in range mid,... last , so returning -1 is correct.  If line 7 is false, then line 10 is executed and the argument is similar.