Last active
June 9, 2019 20:44
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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