Binary search

Binary search algorithm is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array; if they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful. If the search ends with the remaining half being empty, the target is not in the array.
Overview[edit]
Binary search operates on the principle of dividing the search interval in half with each step. It can be implemented in two ways: iteratively or recursively. The algorithm assumes that the array is sorted in ascending or descending order. It is much more efficient than linear search for large arrays, as its time complexity is O(log n), where n is the number of elements in the array.
Algorithm[edit]
The binary search algorithm works as follows:
- Find the middle element of the array.
- If the middle element is equal to the target, the search is complete.
- If the target is less than the middle element, repeat the search on the left subarray.
- If the target is greater than the middle element, repeat the search on the right subarray.
This process is repeated until the target value is found or the subarray becomes empty.
Implementation[edit]
Pseudocode[edit]
The following is a pseudocode representation of the binary search algorithm:
``` function binarySearch(arr, target):
left = 0
right = arr.length - 1
while left <= right:
mid = left + (right - left) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
```
Recursive Approach[edit]
The binary search algorithm can also be implemented recursively:
``` function recursiveBinarySearch(arr, left, right, target):
if right >= left:
mid = left + (right - left) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
return recursiveBinarySearch(arr, mid + 1, right, target)
else:
return recursiveBinarySearch(arr, left, mid - 1, target)
return -1
```
Applications[edit]
Binary search is used in a wide range of computing applications, including:
- Searching in databases
- Finding the position of an element in a sorted list
- In algorithms that require searching, such as finding the square root of a number
- In data compression
Complexity[edit]
The time complexity of binary search is O(log n), making it significantly more efficient for large datasets than a linear search, which has a time complexity of O(n). Its space complexity is O(1) for the iterative approach and O(log n) for the recursive approach due to the stack space used by recursion.
See Also[edit]
Binary search gallery[edit]
-
Binary Search Depiction
-
Binary search work
-
Approximate binary search
-
Binary search example tree
-
Binary search complexity
-
Binary search tree search 4
-
Uniform binary search
-
Exponential search
-
Interpolation search
-
Fractional cascading
Ad. Transform your health with W8MD Weight Loss, Sleep & MedSpa

Tired of being overweight?
Special offer:
Budget GLP-1 weight loss medications
- Semaglutide starting from $29.99/week and up with insurance for visit of $59.99 and up per week self pay.
- Tirzepatide starting from $45.00/week and up (dose dependent) or $69.99/week and up self pay
✔ Same-week appointments, evenings & weekends
Learn more:
- GLP-1 weight loss clinic NYC
- W8MD's NYC medical weight loss
- W8MD Philadelphia GLP-1 shots
- Philadelphia GLP-1 injections
- Affordable GLP-1 shots NYC
- Budget GLP-1 shots
|
WikiMD Medical Encyclopedia |
Medical Disclaimer: WikiMD is for informational purposes only and is not a substitute for professional medical advice. Content may be inaccurate or outdated and should not be used for diagnosis or treatment. Always consult your healthcare provider for medical decisions. Verify information with trusted sources such as CDC.gov and NIH.gov. By using this site, you agree that WikiMD is not liable for any outcomes related to its content. See full disclaimer.
Credits:Most images are courtesy of Wikimedia commons, and templates, categories Wikipedia, licensed under CC BY SA or similar.
Translate this page: - East Asian
中文,
日本,
한국어,
South Asian
हिन्दी,
தமிழ்,
తెలుగు,
Urdu,
ಕನ್ನಡ,
Southeast Asian
Indonesian,
Vietnamese,
Thai,
မြန်မာဘာသာ,
বাংলা
European
español,
Deutsch,
français,
Greek,
português do Brasil,
polski,
română,
русский,
Nederlands,
norsk,
svenska,
suomi,
Italian
Middle Eastern & African
عربى,
Turkish,
Persian,
Hebrew,
Afrikaans,
isiZulu,
Kiswahili,
Other
Bulgarian,
Hungarian,
Czech,
Swedish,
മലയാളം,
मराठी,
ਪੰਜਾਬੀ,
ગુજરાતી,
Portuguese,
Ukrainian