SlideShare a Scribd company logo
1 of 20
Analysis of Algorithm
BINARY SEARCH
Presented by
Raunak, Ankush, Sukalyan, Aziza,
Sweta & Hasibul
CONTENTS
1. Introduction
2. Algorithm
3. Example
4. Time Complexity
5. Advantages and Disadvantages
6. Applications
Binary Search 2
INTRODUCTION
 Binary Search is the search technique that works efficiently on sorted lists. Hence, to
search an element into some list using the binary search technique, we must ensure
that the list is sorted.
 Binary search follows the divide and conquer approach in which the list is divided into
two halves, and the item is compared with the middle element of the list. If the match
is found then, the location of the middle element is returned. Otherwise, we search
into either of the halves depending upon the result produced through the match.
Binary Search 3
 Binary Search is also known as half-interval search or logarithmic search, is a search
algorithm that finds the position of 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 algorithm; and
using only constant {(O(1))} space.
Binary Search 4
 Difference between Linear Search and Binary Search –
Binary Search 5
Basis of Comparison Linear Search Binary Search
Definition The linear search starts
searching from the first
element and compares each
element with a searched
element till the element is not
found.
It finds the position of the
searched element by finding the
middle element of the array.
Approach It is based on the sequential
approach.
It is based on the divide and
conquer approach.
Size It is preferrable for the small-
sized data sets.
It is preferrable for the large-size
data sets.
Efficiency It is less efficient in the case
of large-size data sets.
It is more efficient in the case of
large-size data sets.
Sorted data In a linear search, the
elements don't need to be
arranged in sorted order.
The pre-condition for the binary
search is that the elements must
be arranged in a sorted order.
ALGORITHM
 There are two methods to implement the binary search algorithm –
 Iterative Method:
o If the target value is equal to the middle element, its position in the array is
returned.
o If the target value is less than the middle element, then we continue the
search in the lower half of the array.
o If the target value is greater than the middle element, then we continue
the search in the upper half of the array.
 Recursive Method:
o Recursive implementation of binary search algorithm, in the method
binarySearch(), follows almost the same logic as iterative version, except
for a couple of differences.
Binary Search 6
Now, let’s see the algorithm of binary search –
Binary_Search(a, lower_bound, upper_bound, val)
Step 1: set start = lower_bound, end = upper_bound, pos = - 1
Step 2: repeat steps 3 and 4 while start <=end
Step 3: set mid = (start + end)/2
Step 4: if a[mid] = val
set pos = mid
print pos
Binary Search 7
go to step 6
else if a[mid] > val
set end = mid - 1
else
set start = mid + 1
[end of if]
[end of loop]
Step 5: if pos = -1
print "value is not present in the array"
[end of if]
Step 6: exit
Binary Search 8
EXAMPLE
Let the elements of array are –
Let the element to search is, K=56
We have to use the below formula to calculate the mid of the array –
mid=(start+end)/2
So, in the given array –
start = 0, end = 8 and mid = (0+8)/2 = 4
So, 4 is the mid of the array.
Binary Search 9
So, mid=4
Binary Search 10
Now, the element to search is found. So algorithm will return the index of the element
matched.
Binary Search 11
TIME COMPLEXITY
Now, let's see the time complexity of Binary search in the best case, average case, and
worst case.
1. Best Case –
 The element to be search is in the middle of the list.
 In this case, the element is found in the first step itself and this involves 1
comparison.
 Therefore, the Best Case Time Complexity of Binary Search is O(1).
Binary Search 12
2. Average Case –
 The average case time complexity of Binary search is O(log n).
3. Worst Case –
 The element is to search is in the first index or last index.
 In this case, the total number of comparisons required is {log n} comparisons.
 Therefore, the worst case time complexity of Binary Search is O(log n).
Binary Search 13
Time Complexity:
Space Complexity
 The Space Complexity of Binary Search is O(1).
Binary Search 14
Case Time Complexity
Best Case O(1)
Average Case O(log n)
Worst Case O(log n)
Space Complexity O(1)
ADVANTAGES & DISADVANTAGES
 Advantages of Binary Search:
 It is better than a linear search algorithm since its run time complexity is O(log
n).
 At each iteration, the binary search algorithm eliminates half of the list and
significantly reduces the search space.
 The binary search algorithm works even when the array is rotated by some
position and finds the target element.
Binary Search 15
 Disadvantages of Binary Search:
 The recursive method uses stack space.
 Binary search is error-prone. Some of the common errors are as follows:
 Off-by-one errors: While determining the boundary of the next interval, there
might be overlapping errors.
o Handling of duplicate items: While returning the first item, it might be
possible we return a subsequence similar item.
o Numerical underflows/overflows: In huge arrays when computing indices.
There might be overflows
o Recursive vs non-recursive implementation, which should be considered
while designing as recursive takes stack space.
Binary Search 16
APPLICATIONS
The applications of Binary Search are:
1. Find an element in a sorted array
2. Applications of Binary Search beyond arrays
 To find if n is a square of an integer
 Find the first value greater than or equal to x in a given array of sorted
integers
 Find the frequency of a given target value in an array of integers
 Find the peak of an array which increases and then decreases
 A sorted array is rotated n times. Search for a target value in the array
Binary Search 17
3. Real life applications of Binary Search –
 Dictionary
 Debugging a linear piece of code
 Figuring out resource requirements
for a large system
 Find values in sorted collection
 Semiconductor test programs
 Numerical solutions to an equation
18
Binary Search
REFERENCE
 GeeksForGeeks [https://www.geeksforgeeks.org/]
 Javatpoint [https://www.javatpoint.com/]
 SlideShare [https://www.slideshare.net/]
Binary Search 19
THANK YOU!
Binary Search 20

More Related Content

What's hot

What's hot (20)

Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
 
Selection sorting
Selection sortingSelection sorting
Selection sorting
 
Binary search in ds
Binary search in dsBinary search in ds
Binary search in ds
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Binary search in data structure
Binary search in data structureBinary search in data structure
Binary search in data structure
 
Searching
SearchingSearching
Searching
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
 
AI Lecture 3 (solving problems by searching)
AI Lecture 3 (solving problems by searching)AI Lecture 3 (solving problems by searching)
AI Lecture 3 (solving problems by searching)
 
Problem solving agents
Problem solving agentsProblem solving agents
Problem solving agents
 
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
 
Binary search
Binary searchBinary search
Binary search
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
Binary search Algorithm
Binary search AlgorithmBinary search Algorithm
Binary search Algorithm
 
A* Search Algorithm
A* Search AlgorithmA* Search Algorithm
A* Search Algorithm
 

Similar to Analysis of Algorithm - Binary Search.pptx

advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdfharamaya university
 
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
 
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
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searchingsajinis3
 
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
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Abdul Khan
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sortingguest2cb109
 
IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET Journal
 
Searching,sorting
Searching,sortingSearching,sorting
Searching,sortingLavanyaJ28
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithmmaamir farooq
 
Data operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsData operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsAnushdika Jeganathan
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal searchKrish_ver2
 
7 searching injava-binary
7 searching injava-binary7 searching injava-binary
7 searching injava-binaryirdginfo
 

Similar to Analysis of Algorithm - Binary Search.pptx (20)

advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
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
 
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
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 
Ch05 Black Jack
Ch05  Black  JackCh05  Black  Jack
Ch05 Black Jack
 
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++
 
Data structure unit I part B
Data structure unit I part BData structure unit I part B
Data structure unit I part B
 
Binary.pptx
Binary.pptxBinary.pptx
Binary.pptx
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
 
IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching Algorithms
 
Searching,sorting
Searching,sortingSearching,sorting
Searching,sorting
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithm
 
Data operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsData operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithms
 
arrays in c
arrays in carrays in c
arrays in c
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
 
7 searching injava-binary
7 searching injava-binary7 searching injava-binary
7 searching injava-binary
 
Non-Uniform Gap Distribution Library Sort
Non-Uniform Gap Distribution Library SortNon-Uniform Gap Distribution Library Sort
Non-Uniform Gap Distribution Library Sort
 

More from Maulana Abul Kalam Azad University of Technology (12)

Introduction to Internet of Things (IoT)
Introduction to Internet of Things (IoT) Introduction to Internet of Things (IoT)
Introduction to Internet of Things (IoT)
 
IOT BASED RFID SMART DOOR LOCK SYSTEM USING NODE MCU - MAJOR.pptx
IOT BASED RFID SMART DOOR LOCK SYSTEM USING NODE MCU - MAJOR.pptxIOT BASED RFID SMART DOOR LOCK SYSTEM USING NODE MCU - MAJOR.pptx
IOT BASED RFID SMART DOOR LOCK SYSTEM USING NODE MCU - MAJOR.pptx
 
SMART SECURITY SYSTEM USING IOT - MID-TERM PROJECT.pptx
SMART SECURITY SYSTEM USING IOT - MID-TERM PROJECT.pptxSMART SECURITY SYSTEM USING IOT - MID-TERM PROJECT.pptx
SMART SECURITY SYSTEM USING IOT - MID-TERM PROJECT.pptx
 
Interprocess Communication.pptx
Interprocess Communication.pptxInterprocess Communication.pptx
Interprocess Communication.pptx
 
Green house gases & effects
Green house gases & effectsGreen house gases & effects
Green house gases & effects
 
8085 Microprocessor
8085 Microprocessor8085 Microprocessor
8085 Microprocessor
 
Samsung Galaxy S9+
Samsung Galaxy S9+Samsung Galaxy S9+
Samsung Galaxy S9+
 
5G Wireless Technology
5G Wireless Technology5G Wireless Technology
5G Wireless Technology
 
Normalization in DBMS
Normalization in DBMSNormalization in DBMS
Normalization in DBMS
 
Introduction to manufacturing process
Introduction to manufacturing processIntroduction to manufacturing process
Introduction to manufacturing process
 
Samsung Galaxy S8 Plus
Samsung Galaxy S8 PlusSamsung Galaxy S8 Plus
Samsung Galaxy S8 Plus
 
Robotic Science
Robotic ScienceRobotic Science
Robotic Science
 

Recently uploaded

RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998YohFuh
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfSocial Samosa
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfRachmat Ramadhan H
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
 
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...Suhani Kapoor
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...Suhani Kapoor
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...shivangimorya083
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSAishani27
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz1
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 

Recently uploaded (20)

RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
 
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICS
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signals
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 

Analysis of Algorithm - Binary Search.pptx

  • 1. Analysis of Algorithm BINARY SEARCH Presented by Raunak, Ankush, Sukalyan, Aziza, Sweta & Hasibul
  • 2. CONTENTS 1. Introduction 2. Algorithm 3. Example 4. Time Complexity 5. Advantages and Disadvantages 6. Applications Binary Search 2
  • 3. INTRODUCTION  Binary Search is the search technique that works efficiently on sorted lists. Hence, to search an element into some list using the binary search technique, we must ensure that the list is sorted.  Binary search follows the divide and conquer approach in which the list is divided into two halves, and the item is compared with the middle element of the list. If the match is found then, the location of the middle element is returned. Otherwise, we search into either of the halves depending upon the result produced through the match. Binary Search 3
  • 4.  Binary Search is also known as half-interval search or logarithmic search, is a search algorithm that finds the position of 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 algorithm; and using only constant {(O(1))} space. Binary Search 4
  • 5.  Difference between Linear Search and Binary Search – Binary Search 5 Basis of Comparison Linear Search Binary Search Definition The linear search starts searching from the first element and compares each element with a searched element till the element is not found. It finds the position of the searched element by finding the middle element of the array. Approach It is based on the sequential approach. It is based on the divide and conquer approach. Size It is preferrable for the small- sized data sets. It is preferrable for the large-size data sets. Efficiency It is less efficient in the case of large-size data sets. It is more efficient in the case of large-size data sets. Sorted data In a linear search, the elements don't need to be arranged in sorted order. The pre-condition for the binary search is that the elements must be arranged in a sorted order.
  • 6. ALGORITHM  There are two methods to implement the binary search algorithm –  Iterative Method: o If the target value is equal to the middle element, its position in the array is returned. o If the target value is less than the middle element, then we continue the search in the lower half of the array. o If the target value is greater than the middle element, then we continue the search in the upper half of the array.  Recursive Method: o Recursive implementation of binary search algorithm, in the method binarySearch(), follows almost the same logic as iterative version, except for a couple of differences. Binary Search 6
  • 7. Now, let’s see the algorithm of binary search – Binary_Search(a, lower_bound, upper_bound, val) Step 1: set start = lower_bound, end = upper_bound, pos = - 1 Step 2: repeat steps 3 and 4 while start <=end Step 3: set mid = (start + end)/2 Step 4: if a[mid] = val set pos = mid print pos Binary Search 7
  • 8. go to step 6 else if a[mid] > val set end = mid - 1 else set start = mid + 1 [end of if] [end of loop] Step 5: if pos = -1 print "value is not present in the array" [end of if] Step 6: exit Binary Search 8
  • 9. EXAMPLE Let the elements of array are – Let the element to search is, K=56 We have to use the below formula to calculate the mid of the array – mid=(start+end)/2 So, in the given array – start = 0, end = 8 and mid = (0+8)/2 = 4 So, 4 is the mid of the array. Binary Search 9
  • 11. Now, the element to search is found. So algorithm will return the index of the element matched. Binary Search 11
  • 12. TIME COMPLEXITY Now, let's see the time complexity of Binary search in the best case, average case, and worst case. 1. Best Case –  The element to be search is in the middle of the list.  In this case, the element is found in the first step itself and this involves 1 comparison.  Therefore, the Best Case Time Complexity of Binary Search is O(1). Binary Search 12
  • 13. 2. Average Case –  The average case time complexity of Binary search is O(log n). 3. Worst Case –  The element is to search is in the first index or last index.  In this case, the total number of comparisons required is {log n} comparisons.  Therefore, the worst case time complexity of Binary Search is O(log n). Binary Search 13
  • 14. Time Complexity: Space Complexity  The Space Complexity of Binary Search is O(1). Binary Search 14 Case Time Complexity Best Case O(1) Average Case O(log n) Worst Case O(log n) Space Complexity O(1)
  • 15. ADVANTAGES & DISADVANTAGES  Advantages of Binary Search:  It is better than a linear search algorithm since its run time complexity is O(log n).  At each iteration, the binary search algorithm eliminates half of the list and significantly reduces the search space.  The binary search algorithm works even when the array is rotated by some position and finds the target element. Binary Search 15
  • 16.  Disadvantages of Binary Search:  The recursive method uses stack space.  Binary search is error-prone. Some of the common errors are as follows:  Off-by-one errors: While determining the boundary of the next interval, there might be overlapping errors. o Handling of duplicate items: While returning the first item, it might be possible we return a subsequence similar item. o Numerical underflows/overflows: In huge arrays when computing indices. There might be overflows o Recursive vs non-recursive implementation, which should be considered while designing as recursive takes stack space. Binary Search 16
  • 17. APPLICATIONS The applications of Binary Search are: 1. Find an element in a sorted array 2. Applications of Binary Search beyond arrays  To find if n is a square of an integer  Find the first value greater than or equal to x in a given array of sorted integers  Find the frequency of a given target value in an array of integers  Find the peak of an array which increases and then decreases  A sorted array is rotated n times. Search for a target value in the array Binary Search 17
  • 18. 3. Real life applications of Binary Search –  Dictionary  Debugging a linear piece of code  Figuring out resource requirements for a large system  Find values in sorted collection  Semiconductor test programs  Numerical solutions to an equation 18 Binary Search
  • 19. REFERENCE  GeeksForGeeks [https://www.geeksforgeeks.org/]  Javatpoint [https://www.javatpoint.com/]  SlideShare [https://www.slideshare.net/] Binary Search 19