SlideShare a Scribd company logo
1 of 16
Download to read offline
Binary Search
comparison and
analysis
Binary Search
comparison and
analysis
RAMKRISHNA BHAGAT
MCA-2nd
Year
What is searching?
●
In computer science, searching is the process of finding an item
with specified properties from a collection of items. The items
may be stored as records in a database, simple data elements in
arrays, text in files, nodes in trees, vertices and edges in graphs,
or maybe be elements in other search place.
●
The definition of a search is the process of looking for
something or someone.
●
Example : An example of a search is a quest to find a missing
person.
2
2
●
Searchingis one of the core computer science algorithms. We know
that today’s computers store a lot of information. To retrieve this
information proficiently we need very efficient searching
algorithms.
Why do we need searching?
Types of Searching
Linear search
Binary search
2
3
Disadvantages of Linear Search
4
●
Linear search is slower then other
searching algorithm.
●
If the array is small then it is good to use
linear search.
●
If the array size is large then it is not good
to use linear search because linear search
takes a large amount of time to search as
the element is compared sequentially with
each and every element .
What is Binary Search ?
• Binary search is searching technique
which uses divide and conquer approach.
• In binary search we divide the array in two halves.
We find the element present at the middle index of
the array. Now we compare the element to be
searched with the element present at the middle
index.
5
●
We perform binary search only on a
sorted array. If the array is not sorted
the binary search fails to work.
Continued….
• If the element to be searched is less than the
element at the middle index ,we search in the left
half of the array otherwise we will search in the
right half.
6
●
We continue this process until
we find our element to be
searched.
●
Because binary search is faster than linear
search.
●
It is efficient because it eliminates half items at
each step: ½ then ¼ then 1/8.
●
It is also useful when we have a large array to be
searched whereas in linear search , we have to
search the whole array.
7
Why we need Binary Search ?
Example
2
8
9
10
11
Algorithm of Binary search
12
Step 1: Find middle element of the array.
Step 2: Compare the value of the middle element with the target value.
Step 3: If they match, it is returned.
Step 4: If the value is less or greater than the target, the search continues in the lower or
upper half of the array accordingly.
Step 5: The same procedure as in step 2-4 continues, but with a smaller part of the array. This
continues until the target element is found or until there are no elements left.
12
13
Disadvantages of Binary Search
●
In binary search the first and the
foremost thing is that the array should
be sorted.
●
If the array is not sorted then we have
to sort it first and then we have to
perform binary search. Because of this
the complexity of binary search will
increase.
13
Comparison between Linear and Binary Search
●
Linear search uses sequential approach.
●
Linear search slow working.
● Linear search does the sequential access
●
In linear search, the worst case complexity
is O(n)
●
Input data need not to be sorted in Linear
Search.
●
Binary search implements divide and
conquer approach.
●
Binary search is better and quite faster
●
Binary search access data randomly.
●
Binary search worst case complexity is
O(log n) .
●
Input data needs to be sorted in Binary
Search
14
Complexities of Linear and Binary Search
●
Since in Linear search we
compare the target value
with each and every
element of the array ,
therefore the complexity of
linear search is O(n).
●
At each iteration binary search
divides the array in two halves
so it is taking logarithmic time
to search the array. Therefore
the complexity of binary
search is O(log2 (n)).
15
16
Thanks!

More Related Content

What's hot

12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMSkoolkampus
 
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 StructureBalwant Gorad
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search treeKrish_ver2
 
Insertion sort
Insertion sortInsertion sort
Insertion sortalmaqboli
 
04 brute force
04 brute force04 brute force
04 brute forceHira Gul
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithmNeoClassical
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sortDistrict Administration
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IMohamed Loey
 
Selection sort
Selection sortSelection sort
Selection sortstella D
 
Hash table
Hash tableHash table
Hash tableVu Tran
 
Bubble sort | Data structure |
Bubble sort | Data structure |Bubble sort | Data structure |
Bubble sort | Data structure |MdSaiful14
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sortingKaushal Shah
 

What's hot (20)

12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS
 
Chapter18
Chapter18Chapter18
Chapter18
 
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
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Ppt bubble sort
Ppt bubble sortPpt bubble sort
Ppt bubble sort
 
Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point
 
Selection sort
Selection sortSelection sort
Selection sort
 
04 brute force
04 brute force04 brute force
04 brute force
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Selection sort
Selection sortSelection sort
Selection sort
 
Hash table
Hash tableHash table
Hash table
 
Bubble sort | Data structure |
Bubble sort | Data structure |Bubble sort | Data structure |
Bubble sort | Data structure |
 
Binary search
Binary searchBinary search
Binary search
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 

Similar to Binary search

Searching, Sorting and Hashing Techniques
Searching, Sorting and Hashing TechniquesSearching, Sorting and Hashing Techniques
Searching, Sorting and Hashing TechniquesSelvaraj Seerangan
 
programming in C
programming in Cprogramming in C
programming in CADITHYAM19
 
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
 
Rahat & juhith
Rahat & juhithRahat & juhith
Rahat & juhithRj Juhith
 
Data structure and algorithms
Data structure and algorithmsData structure and algorithms
Data structure and algorithmstechnologygyan
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operationsbabuk110
 
Data Structures and Algorithm - Week 9 - Search Algorithms
Data Structures and Algorithm - Week 9 - Search AlgorithmsData Structures and Algorithm - Week 9 - Search Algorithms
Data Structures and Algorithm - Week 9 - Search AlgorithmsFerdin Joe John Joseph PhD
 
Data operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsData operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsAnushdika Jeganathan
 
SORTING techniques.pptx
SORTING techniques.pptxSORTING techniques.pptx
SORTING techniques.pptxDr.Shweta
 
Binary search algorithm.pptx
Binary search  algorithm.pptxBinary search  algorithm.pptx
Binary search algorithm.pptxbhuvansaachi18
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHISowmya Jyothi
 
Presentation on Searching and Sorting in C language.pptx
Presentation on Searching and Sorting in C language.pptxPresentation on Searching and Sorting in C language.pptx
Presentation on Searching and Sorting in C language.pptxKrishnanandmishra15
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searchingsajinis3
 

Similar to Binary search (20)

Searching, Sorting and Hashing Techniques
Searching, Sorting and Hashing TechniquesSearching, Sorting and Hashing Techniques
Searching, Sorting and Hashing Techniques
 
programming in C
programming in Cprogramming in C
programming in C
 
searching
searchingsearching
searching
 
Amitdsppt
AmitdspptAmitdsppt
Amitdsppt
 
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
 
Rahat & juhith
Rahat & juhithRahat & juhith
Rahat & juhith
 
Data structure and algorithms
Data structure and algorithmsData structure and algorithms
Data structure and algorithms
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
 
Data Structures and Algorithm - Week 9 - Search Algorithms
Data Structures and Algorithm - Week 9 - Search AlgorithmsData Structures and Algorithm - Week 9 - Search Algorithms
Data Structures and Algorithm - Week 9 - Search Algorithms
 
Data operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsData operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithms
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Data structure unit I part B
Data structure unit I part BData structure unit I part B
Data structure unit I part B
 
SORTING techniques.pptx
SORTING techniques.pptxSORTING techniques.pptx
SORTING techniques.pptx
 
seaching internal 2 ppt
seaching internal 2 pptseaching internal 2 ppt
seaching internal 2 ppt
 
Database Engine
Database EngineDatabase Engine
Database Engine
 
Binary search algorithm.pptx
Binary search  algorithm.pptxBinary search  algorithm.pptx
Binary search algorithm.pptx
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
 
Presentation on Searching and Sorting in C language.pptx
Presentation on Searching and Sorting in C language.pptxPresentation on Searching and Sorting in C language.pptx
Presentation on Searching and Sorting in C language.pptx
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 

More from Ramkrishna bhagat (7)

JAVA APPLETS
JAVA APPLETSJAVA APPLETS
JAVA APPLETS
 
DBTG MODEL
DBTG MODELDBTG MODEL
DBTG MODEL
 
IPV6 , ipv6 ,TCP/IP, TCP ,IP
IPV6 , ipv6 ,TCP/IP, TCP ,IPIPV6 , ipv6 ,TCP/IP, TCP ,IP
IPV6 , ipv6 ,TCP/IP, TCP ,IP
 
Data warehouse
Data warehouseData warehouse
Data warehouse
 
Graph Representation
Graph RepresentationGraph Representation
Graph Representation
 
22 ramkrishna
22 ramkrishna22 ramkrishna
22 ramkrishna
 
E-Ball Technology
E-Ball TechnologyE-Ball Technology
E-Ball Technology
 

Recently uploaded

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 

Recently uploaded (20)

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 

Binary search

  • 1. Binary Search comparison and analysis Binary Search comparison and analysis RAMKRISHNA BHAGAT MCA-2nd Year
  • 2. What is searching? ● In computer science, searching is the process of finding an item with specified properties from a collection of items. The items may be stored as records in a database, simple data elements in arrays, text in files, nodes in trees, vertices and edges in graphs, or maybe be elements in other search place. ● The definition of a search is the process of looking for something or someone. ● Example : An example of a search is a quest to find a missing person. 2 2
  • 3. ● Searchingis one of the core computer science algorithms. We know that today’s computers store a lot of information. To retrieve this information proficiently we need very efficient searching algorithms. Why do we need searching? Types of Searching Linear search Binary search 2 3
  • 4. Disadvantages of Linear Search 4 ● Linear search is slower then other searching algorithm. ● If the array is small then it is good to use linear search. ● If the array size is large then it is not good to use linear search because linear search takes a large amount of time to search as the element is compared sequentially with each and every element .
  • 5. What is Binary Search ? • Binary search is searching technique which uses divide and conquer approach. • In binary search we divide the array in two halves. We find the element present at the middle index of the array. Now we compare the element to be searched with the element present at the middle index. 5 ● We perform binary search only on a sorted array. If the array is not sorted the binary search fails to work.
  • 6. Continued…. • If the element to be searched is less than the element at the middle index ,we search in the left half of the array otherwise we will search in the right half. 6 ● We continue this process until we find our element to be searched.
  • 7. ● Because binary search is faster than linear search. ● It is efficient because it eliminates half items at each step: ½ then ¼ then 1/8. ● It is also useful when we have a large array to be searched whereas in linear search , we have to search the whole array. 7 Why we need Binary Search ?
  • 9. 9
  • 10. 10
  • 11. 11
  • 12. Algorithm of Binary search 12 Step 1: Find middle element of the array. Step 2: Compare the value of the middle element with the target value. Step 3: If they match, it is returned. Step 4: If the value is less or greater than the target, the search continues in the lower or upper half of the array accordingly. Step 5: The same procedure as in step 2-4 continues, but with a smaller part of the array. This continues until the target element is found or until there are no elements left. 12
  • 13. 13 Disadvantages of Binary Search ● In binary search the first and the foremost thing is that the array should be sorted. ● If the array is not sorted then we have to sort it first and then we have to perform binary search. Because of this the complexity of binary search will increase. 13
  • 14. Comparison between Linear and Binary Search ● Linear search uses sequential approach. ● Linear search slow working. ● Linear search does the sequential access ● In linear search, the worst case complexity is O(n) ● Input data need not to be sorted in Linear Search. ● Binary search implements divide and conquer approach. ● Binary search is better and quite faster ● Binary search access data randomly. ● Binary search worst case complexity is O(log n) . ● Input data needs to be sorted in Binary Search 14
  • 15. Complexities of Linear and Binary Search ● Since in Linear search we compare the target value with each and every element of the array , therefore the complexity of linear search is O(n). ● At each iteration binary search divides the array in two halves so it is taking logarithmic time to search the array. Therefore the complexity of binary search is O(log2 (n)). 15