Binary search

From WikiMD's medical encyclopedia

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

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

The binary search algorithm works as follows:

  1. Find the middle element of the array.
  2. If the middle element is equal to the target, the search is complete.
  3. If the target is less than the middle element, repeat the search on the left subarray.
  4. 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

Pseudocode

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

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

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

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

Stub icon
   This article is a computer science stub. You can help WikiMD by expanding it!



Binary search gallery

Navigation: Wellness - Encyclopedia - Health topics - Disease Index‏‎ - Drugs - World Directory - Gray's Anatomy - Keto diet - Recipes

Transform your life with W8MD's budget GLP-1 injections from $125.

W8mdlogo.png
W8MD weight loss doctors team

W8MD offers a medical weight loss program to lose weight in Philadelphia. Our physician-supervised medical weight loss provides:

NYC weight loss doctor appointments

Start your NYC weight loss journey today at our NYC medical weight loss and Philadelphia medical weight loss clinics.

Linkedin_Shiny_Icon Facebook_Shiny_Icon YouTube_icon_(2011-2013) Google plus


Advertise on WikiMD

WikiMD's Wellness Encyclopedia

Let Food Be Thy Medicine
Medicine Thy Food - Hippocrates

Medical Disclaimer: WikiMD is not a substitute for professional medical advice. The information on WikiMD is provided as an information resource only, may be incorrect, outdated or misleading, and is not to be used or relied on for any diagnostic or treatment purposes. Please consult your health care provider before making any healthcare decisions or for guidance about a specific medical condition. WikiMD expressly disclaims responsibility, and shall have no liability, for any damages, loss, injury, or liability whatsoever suffered as a result of your reliance on the information contained in this site. By visiting this site you agree to the foregoing terms and conditions, which may from time to time be changed or supplemented by WikiMD. If you do not agree to the foregoing terms and conditions, you should not enter or use this site. See full disclaimer.
Credits:Most images are courtesy of Wikimedia commons, and templates, categories Wikipedia, licensed under CC BY SA or similar.

Contributors: Prab R. Tumpati, MD