SlideShare una empresa de Scribd logo
1 de 13
Binary Search
 Name : Kunj Desai
 Enrollment No. :140950107022
 Semester : 5th
 Year : 2016
Introduction
 The Binary Search can be implemented on only
sorted list of elements.
 The process starts with finding the element at the
middle of the list and comparing the key value with the
element in the middle of the list, if the value of the key
is less than the element at the middle then the search
process the key to the list up to the middle element
and if the value of the key is greater than element at
the middle then search the key from middle element to
the end of the list.
Introduction
  Binary search, also known as half-interval
search or logarithmic search, is a search
algorithm that finds the position of a target value
within a sorted array.
 Binary search runs in at worst logarithmic time ,
making {O(log n)} comparisons, where {n} is the
number of elements in the array and {log } is
the binary logarithm ; and using only
constant {(O(1))}space.
Algorithm
Binary Search ( A[0 1 2 3…….n-1] , key)
low 0
high n-1
while(low <= high)
do
{
m(low + high)/2
if (key=A[m])
then
Algorithm
return m
else if (key<A[m])
then
high m-1
else
low m+1
}
Example
 Let the number givens be 4,6,7,9,10 and the key is 9.
 So , according to the algorithm these numbers are to
be stored in a 1D array named A[0 1 2….n-1];where
n= number of elements i.e. in this example 5.
 Thus according to the algorithm A[0 1 2 3 4] is
generated and key value is passed in the array that is
9.
Binary Search( A[0 1 2 3 4] , 9 )
low=0
high=4
while(0<=4)
Example
Iteration 1:
m=2
low=3 //here key=9 is greater than A[m]=7 ,thus it
executes last else condition .
Since,(3<4) loop will continue.
Iteration 2:
m=3
return 3;//Since key=9 is equal to A[m]=9 , where m=3
, it returns 3 and thus ends its binary search
here by returning the index where the key
value is stored.
Time Analysis
 Suppose we have an array A and in this array we are
searching for a value K. If A has no special properties,
then there is no better way to search K than linear
search -- to start at the beginning and go through the
array one step at a time, comparing each element to K
in turn. The time it takes (on average, and in the worst
case) is linear, or O(N), to the number of items of
array.
 But if A is a sorted array, there is a much faster way,
Binary Search, to look for K. In binary search, after
each iteration, the length of the array we are looking in
gets cut in half.
Time Analysis
 Binary Search can be analyzed with the best, worst,
and average case number of comparisons. These
analyses are dependent upon the length of the array,
so let N =|A| denote the length of the Array A.
 The numbers of comparisons for the recursive and
iterative versions of Binary Search are the same, if
comparison counting is relaxed slightly. For Recursive
Binary Search, count each pass through the if-then-
else block as one comparison. For Iterative Binary
Search, count each pass through the while block as
one comparison.
Time Analysis
 Best case - O (1) comparisons : In the best case, the
item X is the middle in the array A. A constant number
of comparisons (actually just 1) are required.
 Worst case - O (log n) comparisons : In the worst
case, the item X does not exist in the array A at all.
Through each recursion or iteration of Binary Search,
the size of the admissible range is halved. This
halving can be done ceiling(log n ) times. Thus,
ceiling(log n ) comparisons are required.
Time Analysis
 Average case - O (log n) comparisons : To find the
average case, take the sum over all elements of the
product of number of comparisons required to find
each element and the probability of searching for that
element. To simplify the analysis, assume that no item
which is not in A will be searched for, and that the
probabilities of searching for each element are
uniform.
Advantage and Disadvantage
 Advantage:
1. Binary search is an optimal searching algorithm using
which we can search desired element very efficiently.
 Disadvantage:
1. This algorithm requires the list to be sorted . Then
only this method is applicable.
Application
1. The binary search is an efficient searching method
and is used to search desired record from database.
2. For solving nonlinear equations with one unknown
this method is used.
Thank-You

Más contenido relacionado

La actualidad más candente

Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IMohamed Loey
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithmNeoClassical
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure shameen khan
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structureMAHALAKSHMI P
 
Applications of stack
Applications of stackApplications of stack
Applications of stackeShikshak
 
Topological Sorting
Topological SortingTopological Sorting
Topological SortingShahDhruv21
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary treeKrish_ver2
 
Selection sort
Selection sortSelection sort
Selection sortstella D
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursionAbdullah Al-hazmy
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting AlgorithmsPranay Neema
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSGokul Hari
 

La actualidad más candente (20)

Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Selection sorting
Selection sortingSelection sorting
Selection sorting
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Linear Search Presentation
Linear Search PresentationLinear Search Presentation
Linear Search Presentation
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Quick sort
Quick sortQuick sort
Quick sort
 
stack & queue
stack & queuestack & queue
stack & queue
 
Selection sort
Selection sortSelection sort
Selection sort
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Linked list
Linked listLinked list
Linked list
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 

Similar a Binary Search

advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdfharamaya university
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searchingsajinis3
 
Algorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptxAlgorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptxAftabali702240
 
Linear Search
Linear SearchLinear Search
Linear SearchSWATHIR72
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting AlgorithmsRahul Jamwal
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxprakashvs7
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingThenmozhiK5
 
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptxAyanMandal44
 
Demonstrate interpolation search
Demonstrate interpolation searchDemonstrate interpolation search
Demonstrate interpolation searchmanojmanoj218596
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sortingguest2cb109
 

Similar a Binary Search (20)

Binary.pptx
Binary.pptxBinary.pptx
Binary.pptx
 
Analysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptxAnalysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptx
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 
L1803016468
L1803016468L1803016468
L1803016468
 
SEARCHING
SEARCHINGSEARCHING
SEARCHING
 
Algorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptxAlgorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptx
 
Linear Search
Linear SearchLinear Search
Linear Search
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
 
Binary search
Binary searchBinary search
Binary search
 
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
 
Searching.pptx
Searching.pptxSearching.pptx
Searching.pptx
 
Demonstrate interpolation search
Demonstrate interpolation searchDemonstrate interpolation search
Demonstrate interpolation search
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
 
Non-Uniform Gap Distribution Library Sort
Non-Uniform Gap Distribution Library SortNon-Uniform Gap Distribution Library Sort
Non-Uniform Gap Distribution Library Sort
 
Ch05 Black Jack
Ch05  Black  JackCh05  Black  Jack
Ch05 Black Jack
 
Searching_Sorting.pptx
Searching_Sorting.pptxSearching_Sorting.pptx
Searching_Sorting.pptx
 

Más de kunj desai

OLAP operations
OLAP operationsOLAP operations
OLAP operationskunj desai
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsingkunj desai
 
Loaders and Linkers
Loaders and LinkersLoaders and Linkers
Loaders and Linkerskunj desai
 
Introduction to 8085 microprocessor
Introduction to 8085 microprocessorIntroduction to 8085 microprocessor
Introduction to 8085 microprocessorkunj desai
 
Custom Controls in ASP.net
Custom Controls in ASP.netCustom Controls in ASP.net
Custom Controls in ASP.netkunj desai
 
JDBC Connectivity Model
JDBC Connectivity ModelJDBC Connectivity Model
JDBC Connectivity Modelkunj desai
 
Requirement specification (SRS)
Requirement specification (SRS)Requirement specification (SRS)
Requirement specification (SRS)kunj desai
 
Minimization of DFA
Minimization of DFAMinimization of DFA
Minimization of DFAkunj desai
 
php databse handling
php databse handlingphp databse handling
php databse handlingkunj desai
 
Concept of constructors
Concept of constructorsConcept of constructors
Concept of constructorskunj desai
 

Más de kunj desai (10)

OLAP operations
OLAP operationsOLAP operations
OLAP operations
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
 
Loaders and Linkers
Loaders and LinkersLoaders and Linkers
Loaders and Linkers
 
Introduction to 8085 microprocessor
Introduction to 8085 microprocessorIntroduction to 8085 microprocessor
Introduction to 8085 microprocessor
 
Custom Controls in ASP.net
Custom Controls in ASP.netCustom Controls in ASP.net
Custom Controls in ASP.net
 
JDBC Connectivity Model
JDBC Connectivity ModelJDBC Connectivity Model
JDBC Connectivity Model
 
Requirement specification (SRS)
Requirement specification (SRS)Requirement specification (SRS)
Requirement specification (SRS)
 
Minimization of DFA
Minimization of DFAMinimization of DFA
Minimization of DFA
 
php databse handling
php databse handlingphp databse handling
php databse handling
 
Concept of constructors
Concept of constructorsConcept of constructors
Concept of constructors
 

Último

Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 

Último (20)

Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 

Binary Search

  • 1. Binary Search  Name : Kunj Desai  Enrollment No. :140950107022  Semester : 5th  Year : 2016
  • 2. Introduction  The Binary Search can be implemented on only sorted list of elements.  The process starts with finding the element at the middle of the list and comparing the key value with the element in the middle of the list, if the value of the key is less than the element at the middle then the search process the key to the list up to the middle element and if the value of the key is greater than element at the middle then search the key from middle element to the end of the list.
  • 3. Introduction   Binary search, also known as half-interval search or logarithmic search, is a search algorithm that finds the position of a target value within a sorted array.  Binary search runs in at worst logarithmic time , making {O(log n)} comparisons, where {n} is the number of elements in the array and {log } is the binary logarithm ; and using only constant {(O(1))}space.
  • 4. Algorithm Binary Search ( A[0 1 2 3…….n-1] , key) low 0 high n-1 while(low <= high) do { m(low + high)/2 if (key=A[m]) then
  • 5. Algorithm return m else if (key<A[m]) then high m-1 else low m+1 }
  • 6. Example  Let the number givens be 4,6,7,9,10 and the key is 9.  So , according to the algorithm these numbers are to be stored in a 1D array named A[0 1 2….n-1];where n= number of elements i.e. in this example 5.  Thus according to the algorithm A[0 1 2 3 4] is generated and key value is passed in the array that is 9. Binary Search( A[0 1 2 3 4] , 9 ) low=0 high=4 while(0<=4)
  • 7. Example Iteration 1: m=2 low=3 //here key=9 is greater than A[m]=7 ,thus it executes last else condition . Since,(3<4) loop will continue. Iteration 2: m=3 return 3;//Since key=9 is equal to A[m]=9 , where m=3 , it returns 3 and thus ends its binary search here by returning the index where the key value is stored.
  • 8. Time Analysis  Suppose we have an array A and in this array we are searching for a value K. If A has no special properties, then there is no better way to search K than linear search -- to start at the beginning and go through the array one step at a time, comparing each element to K in turn. The time it takes (on average, and in the worst case) is linear, or O(N), to the number of items of array.  But if A is a sorted array, there is a much faster way, Binary Search, to look for K. In binary search, after each iteration, the length of the array we are looking in gets cut in half.
  • 9. Time Analysis  Binary Search can be analyzed with the best, worst, and average case number of comparisons. These analyses are dependent upon the length of the array, so let N =|A| denote the length of the Array A.  The numbers of comparisons for the recursive and iterative versions of Binary Search are the same, if comparison counting is relaxed slightly. For Recursive Binary Search, count each pass through the if-then- else block as one comparison. For Iterative Binary Search, count each pass through the while block as one comparison.
  • 10. Time Analysis  Best case - O (1) comparisons : In the best case, the item X is the middle in the array A. A constant number of comparisons (actually just 1) are required.  Worst case - O (log n) comparisons : In the worst case, the item X does not exist in the array A at all. Through each recursion or iteration of Binary Search, the size of the admissible range is halved. This halving can be done ceiling(log n ) times. Thus, ceiling(log n ) comparisons are required.
  • 11. Time Analysis  Average case - O (log n) comparisons : To find the average case, take the sum over all elements of the product of number of comparisons required to find each element and the probability of searching for that element. To simplify the analysis, assume that no item which is not in A will be searched for, and that the probabilities of searching for each element are uniform.
  • 12. Advantage and Disadvantage  Advantage: 1. Binary search is an optimal searching algorithm using which we can search desired element very efficiently.  Disadvantage: 1. This algorithm requires the list to be sorted . Then only this method is applicable.
  • 13. Application 1. The binary search is an efficient searching method and is used to search desired record from database. 2. For solving nonlinear equations with one unknown this method is used. Thank-You