//The name of this function should not be changed
function main(){
var list = [];
var count = 1000;
for(var i=0;i<count;i++){
list.push(Math.floor(Math.random()*1000));
}
list.sort(function(a, b){return a - b});
var found = recursiveBinarySearch(list,400);
if(found){
print("Found it!");
}else{
print("Not there...");
}
}
function nonrecursiveBinarySearch(list, item){
var bottom = -1;
var top = list.length;
var index = Math.floor((top+bottom)/2);
while(true){
if(item == list[top] || item == list[bottom]){
return true;
}else if(item < list[index]){
top = index;
}else{
bottom = index;
}
index = Math.floor((top+bottom)/2);
if(index == top || index == bottom){
return false;
}
}
return false;
}
//requires the list to be sorted first
function recursiveBinarySearch(list, item){
if(list.length == 1){
if(list[0] == item){
return true;
}else{
return false;
}
}else{
var middleIndex = Math.ceil(list.length/2);
if(item < list[middleIndex]){
return recursiveBinarySearch(list.splice(0,middleIndex));
}else if(item > list[middleIndex]){
return recursiveBinarySearch(list.splice(middleIndex));
}else{
return true;
}
}
}
function sortedSequentialSearch(list, item){
var newList = list.slice(0);
for(var i=0;i<list.length;i++){
if(list[i] == item){
return true;
}else if(list[i] < item){
return false;
}
}
return false;
}
function unsortedSequentialSearch(list, item){
for(var i=0;i<list.length;i++){
if(list[i] == item){
return true;
}
}
return false;
}
/* --------------------------------------------------
The code below should not be changed at all.
--------------------------------------------------- */
var background_input = [];
var background_lineIndex = 0;
$("#runButton").click(function(){
background_input = [];
background_lineIndex = 0;
$("#outputArea").html("");
background_input =...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.