Binary Search Algorithm— A Simple Explanation

A Simple Explanation of Binary Search Algorithm with an Example in Python and GO | Karthikeyan Nagaraj

Karthikeyan Nagaraj
3 min readJan 12, 2023

What is Binary Search?

  • Binary search is a search algorithm that finds the position of a target value within a sorted array.
  • It is also known as half-interval search, logarithmic search, or binary chop

Working Principle:

  • Binary search Algorithm compares the search value to the middle element of the array
  • If it is not equal, then the half in which the target cannot lie is eliminated and the search continues on the remaining half
  • Again taking the middle element to compare to the target value, and repeating this until the target value is found.
  • If the search ends with the remaining half being empty, the target is not in the array.

Steps:

  1. First, we have to sort the Elements In order to perform a Binary search
  2. Then we have to check whether the mid value is equal to the value which we are trying to find
  3. If the mid value is less than the search value then the left side of the array or list is Eliminated
  4. If the mid value is greater than the search value then the right side of the array or list is Eliminated
  5. Then the process is repeated from step 2 until we found the Value

Binary Search Using Python:

elements = [34, 56, 23, 45, 12, 80, 76, 72, 89, 23, 33, 44, 56, 13, 18]

def binary_search(l, n):
l.sort() # used to sort the Elements
low = 0
high = len(l) - 1
mid = 0
i=0
while (i < len(l)):
mid = low + high // 2

if (l[mid] == n): # Checking if the given value is equal to Search value
return mid;
elif (l[mid] < n): # If mid is lower than search value, left side is eliminated
low = mid + 1
elif (l[mid] > n): # If mid is greater than search value, right side is eliminated
high = mid - 1

i+=1
return "Not Found"

res = binary_search(elements,12)
print(res)

Binary Search Using Golang:

package main
import "fmt"

func binarySearch(n int, elements []int) int {

low := 0
high := len(elements) - 1

for low <= high{
mid := (low + high) / 2

if elements[mid] == n {
return mid
} else if elements[mid] < n {
low = mid + 1
} else{
high = mid - 1
}

}

if low == len(elements) || elements[low] != n {
return 0
}

return 1
}

func main(){

items := []int{12, 13, 18, 23, 23, 33, 34, 44, 45, 56, 56, 72, 76, 80, 89}
fmt.Println(binarySearch(40, items))

}

The same Procedure is Followed Here!

  1. Here I have Declared a Function that Takes the “Search Item” and “Elements” as Input and Performs the Binary Search
  2. GO doesn’t have thewhile but Fortunately, we can use for loop to Iterate into the Block of Code

3. Then based on the Conditionmidvalue is Changed

4. If the Item is found then it will return theIndex Value of the Item Else it will return0

Feel Free to Ask Queries via LinkedIn and to Buy me a Cofee : )

Thank you for Reading!!

Happy Coding ~

Author: Karthikeyan Nagaraj ~ Cyberw1ng

python , py , python 3 , pip 3 , go , golang , google , binary search , solution , karthikeyan Nagaraj , cyberw1ng

--

--

Karthikeyan Nagaraj

Security Researcher | Bug Hunter | Web Pentester | CTF Player | TryHackme Top 1% | AI Researcher | Blockchain Developer