Created
May 29, 2013 22:50
-
-
Save lordhumunguz/5674458 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
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)); |
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
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