Skip to content

Briefly explain what is the binary search and how it's work.😎

Notifications You must be signed in to change notification settings

AMJchamod/Binary-Search

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Binary Search

✔Binary search is a searching algorithm that finds the position of a target value within a sorted array.

✔It is a divide-and-conquer algorithm that repeatedly divides the search space in half.

✔Time Complexity:O(log n)

How does Binary Search work?

Compare the middle element of the search space with the key.
• If the key is found at middle element, the process is terminated.
• If the key is not found at middle element, choose which half will be used as the next search space.
• If the key is smaller than the middle element, then the left side is used for next search.
• If the key is larger than the middle element, then the right side is used for next search.

Consider an array arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}, and the target = 23

First Step: Calculate the mid and compare the mid element with the key. If the key is less than mid element, move to left and if it is greater than the mid then move search space to the righ
• Key (i.e., 23) is greater than current mid element (i.e., 16). The search space moves to the right.

Screenshot (13)

• Key is less than the current mid 56. The search space moves to the left

Screenshot (14)

Second Step: If the key matches the value of the mid element, the element is found and stop search

Screenshot (15)

Advantage

✔Fast Search Time
✔Memory Efficiency

Disadvantages

✔Sorted Data Requirement
✔Limited Applicability

About

Briefly explain what is the binary search and how it's work.😎

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages