Skip to content

Instantly share code, notes, and snippets.

@GeorgeErickson
Created May 27, 2011 04:07
Show Gist options
  • Save GeorgeErickson/994626 to your computer and use it in GitHub Desktop.
Save GeorgeErickson/994626 to your computer and use it in GitHub Desktop.
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