Algorithms
Binary Search
Abdelrahman M. Saleh
List of contents
● Introduction
● Example w/ illustrating figures
● Algorithm
● implementation (Java, C++, Python)
● Performance Runtime
○ Best, Average and worst cases.
● Execution
● Other Notes
Introduction
● Binary search works by dividing the array into halfes searching in one with the element
considering the complete array as the first half
● Unlike linear search binary search doesn’t search the whole array .
Example w/ illustrating figures
Algorithm .
● Search a sorted array by repeatedly dividing the search interval in half.
● Begin with an interval covering the whole array.
● If the value of the search key is less than the item in the middle of the interval,
○ narrow the interval to the lower half. Otherwise narrow it to the upper half.
● Repeatedly check until the value is found or the interval is empty.
Implementation .
C++
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l)/2;
if (arr[mid] == x) return mid;
if (arr[mid] > x) return binarySearch(arr, l, mid-1, x);
return binarySearch(arr, mid+1, r, x);
}
return -1;
}
JAVA
static int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l)/2;
if (arr[mid] == x) return mid;
if (arr[mid] > x) return binarySearch(arr, l, mid-1, x);
return binarySearch(arr, mid+1, r, x);
}
return -1;
}
Python .
def binarySearch (arr, l, r, x):
if r >= l:
mid = l + (r - l)/2
if arr[mid] == x:
return mid
elif arr[mid] > x:
return binarySearch(arr, l, mid-1, x)
else:
return binarySearch(arr, mid+1, r, x)
else:
return -1
Performance Runtime
● Time complexity F(n) = F(n/2) + c => Θ(log(n)) .
● Best case : when the element is at midium index .
● Worst case : if the element doesn’t exist .
Execution .
Input array : [1, 2, 3, 4, 6, 7, 9]
Find 2 :
mid index 3 => 2 != 4
Divide array :
Left part [1,2,3] , Right part [6,7,9]
2 > 1 && 2 < 3 => activate left part
mid index 1 => 2 == 2 //found
Element found on index 1
Other Notes .
● Algorithmic Paradigm: Divide and conquer Approach
● Stable: Yes
● Online: Yes
● Bubble search is one of the most of the preferred search algorithms due to it’s simplicity
in code and fast approach to find element .

binary search

  • 1.
  • 2.
    List of contents ●Introduction ● Example w/ illustrating figures ● Algorithm ● implementation (Java, C++, Python) ● Performance Runtime ○ Best, Average and worst cases. ● Execution ● Other Notes
  • 3.
    Introduction ● Binary searchworks by dividing the array into halfes searching in one with the element considering the complete array as the first half ● Unlike linear search binary search doesn’t search the whole array .
  • 4.
  • 5.
    Algorithm . ● Searcha sorted array by repeatedly dividing the search interval in half. ● Begin with an interval covering the whole array. ● If the value of the search key is less than the item in the middle of the interval, ○ narrow the interval to the lower half. Otherwise narrow it to the upper half. ● Repeatedly check until the value is found or the interval is empty.
  • 6.
  • 7.
    C++ int binarySearch(int arr[],int l, int r, int x) { if (r >= l) { int mid = l + (r - l)/2; if (arr[mid] == x) return mid; if (arr[mid] > x) return binarySearch(arr, l, mid-1, x); return binarySearch(arr, mid+1, r, x); } return -1; }
  • 8.
    JAVA static int binarySearch(intarr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l)/2; if (arr[mid] == x) return mid; if (arr[mid] > x) return binarySearch(arr, l, mid-1, x); return binarySearch(arr, mid+1, r, x); } return -1; }
  • 9.
    Python . def binarySearch(arr, l, r, x): if r >= l: mid = l + (r - l)/2 if arr[mid] == x: return mid elif arr[mid] > x: return binarySearch(arr, l, mid-1, x) else: return binarySearch(arr, mid+1, r, x) else: return -1
  • 10.
    Performance Runtime ● Timecomplexity F(n) = F(n/2) + c => Θ(log(n)) . ● Best case : when the element is at midium index . ● Worst case : if the element doesn’t exist .
  • 11.
    Execution . Input array: [1, 2, 3, 4, 6, 7, 9] Find 2 : mid index 3 => 2 != 4 Divide array : Left part [1,2,3] , Right part [6,7,9] 2 > 1 && 2 < 3 => activate left part mid index 1 => 2 == 2 //found Element found on index 1
  • 12.
    Other Notes . ●Algorithmic Paradigm: Divide and conquer Approach ● Stable: Yes ● Online: Yes ● Bubble search is one of the most of the preferred search algorithms due to it’s simplicity in code and fast approach to find element .