Searching Algorithms

by grettynebraska

HTML

<div id="inputDiv" class="left">
Input:<br/>
<textarea id="inputArea"></textarea>
</div>
<div id="buttonDiv" class="left">
<input type="button" id="runButton" value="Run Program">
</div>
<div id="outputDiv" class="left">
Output:<br/>
<textarea id="outputArea"></textarea>
</div>

CSS

.left{
  float: left;
}
.right{
  float: right;
}
.clear{
  clear: both;
}
.hidden{
  display: none;
}
.hint{
  font-size: 75%;
  font-style: italic;
}
#inputDiv, #outputDiv{
  width: 40%;
  padding: 10px;
}
#buttonDiv{
  width: 10%;
  text-align: center;
  padding-top: 30px;
}
#runButton{
  padding: 5px;
}
#inputArea, #outputArea{
  width: 100%;
  height: 200px;
}

/* The switch - the box around the slider */
.switch {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 15px;
}

/* Hide default HTML checkbox */
.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

/* The slider */
.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 11px;
  width: 11px;
  left: 2px;
  bottom: 2px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: #099;
}

input:focus + .slider {
  box-shadow: 0 0 1px #099;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}

JavaScript

//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 =...