Created
May 27, 2011 04:07
-
-
Save GeorgeErickson/994626 to your computer and use it in GitHub Desktop.
This file contains 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
function binarySearch(list, value){ | |
var states = new Array; | |
var state = new Object; | |
state.done = false; | |
state.b = 0; | |
state.e = list.length; | |
state.m = Math.floor((state.b+state.e)/2); | |
//states.push(state.clone()); | |
while(list[state.m] != value && state.b < state.e){ | |
if(value > list[state.m]){ | |
state.b = state.m + 1; | |
} else{ | |
state.e = state.m - 1; | |
} | |
state.m = Math.floor((state.b+state.e)/2) | |
states.push(_.clone(state)); | |
} | |
state.done = true; | |
//repeat the last state with changing done to true | |
states.push(_.clone(state)); | |
return states; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment