Skip to content

Instantly share code, notes, and snippets.

@lordhumunguz
Created May 29, 2013 22:50
Show Gist options
  • Save lordhumunguz/5674458 to your computer and use it in GitHub Desktop.
Save lordhumunguz/5674458 to your computer and use it in GitHub Desktop.
var binarySearch = function(list1, item){
var list = list1.sort();
var startIndex = 0;
var endIndex = list.length-1;
var midIndex = Math.ceil(list.length/2);
while(startIndex !== endIndex){
midIndex = Math.ceil((endIndex+1)/2);
var midVal = list[midIndex];
if (item === midVal) {
return midIndex;
}else if (item < list[midIndex]){
endIndex = midIndex;
endIndex -= 1;
}else if (item > list[midIndex]){
startIndex = midIndex;
startIndex += 1;
};
};
return startIndex;
};
console.log(binarySearch([1,2,3,4,5], 5));
var memory = [];
var Hash = function(){
this.keys = [];
};
Hash.prototype.getIndex = function(key){
var sum = 0
for (var i = 0; i < key.length; i++){
sum += key.charCodeAt(i);
};
return sum;
};
Hash.prototype.setValue = function(key, value){
this.keys.push(key);
var index = this.getIndex(key);
memory[index] = value;
return value
};
Hash.prototype.getValue = function(key){
var index = this.getIndex(key);
return memory[index];
};
Hash.prototype.showAll = function(){
for (var i = 0; i < this.keys.length; i++){
console.log(memory[h.getIndex(h.keys[i])]);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment