Skip to content

Instantly share code, notes, and snippets.

@Dangeranger
Last active June 9, 2019 20:44
Show Gist options
  • Save Dangeranger/d3ad6a09bb5489838a7540330d635bda to your computer and use it in GitHub Desktop.
Save Dangeranger/d3ad6a09bb5489838a7540330d635bda to your computer and use it in GitHub Desktop.
A quick example of the binary search algorithm zeroing in on a secret number.
// Example Binary Search Algorithm
// Secret Number = 17
// Guess is the midpoint of the Max and Min
// e.g. Math.floor((Max - Min) / 2) + 1
// ==============================================================
// Round-1
// Guess = 50
// Answer = 'lower'
// 1 50 100
// Min-----------------------Guess---------------------------- Max
// Low High
// ==============================================================
// Round-2
// Guess = 25
// Answer = 'lower'
// 1 25 50
// Min----------Guess-------------Max
// Low High
// ==============================================================
// Round-3
// Guess = 13
// Answer = 'higher'
// 1 13 25
// Min---Guess----Max
// Low High
// ==============================================================
// Round-4
// Guess = 17
// Answer = 'yes'
// 13 17 25
// Min-Guess-Max
// Low High
// ==============================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment