COP3530 Time Complexity

-Learn the concepts of complexity and Big-O Notation. -Analyze algorithms to determine the time complexity. -Code simple algorithms and evaluate their complexity.

by Neil Daley

HTML

<div id="container">

  Enter Array Size:
  <input type= 'textbox' id= 'size' value= '100' />
  <input type= 'button' value= 'Create Array' style= 'color:white; background-color:blue' onClick= 'createArray();' />
  <br/>
  
  Enter Location:
  <input type= 'textbox' id= 'location' value = '20' />
  <br/> 
  
  Enter Value to Insert:
  <input type="textbox" id="insert" value="99" />
   <input type="button" value="Insert into Array" style= 'color:white; background-color:blue' onClick="insertIntoArray();" />
  <br/>
  
  Enter Value to Find:
  <input type= 'textbox' id= 'find' value= '20' />
  <input type="button" value="Search Array" style= 'color:white; background-color:blue' onClick="findArray();" />
  <br/>
   
  Number of Steps:
  <div id="steps" ></div><br/> 
 
  Output:
  <br/>
  <div id="result"></div>
  <!--<div id="result">This algorithm is O(n)</div>-->
  <div id="aCreate"></div>
  
  <!--<div id= 'aEvent'></div> -->
  
</div>

JavaScript

var aArray = []; // Global scope array
var o = "";			// Global scope Output list 
var c = "0"; 		// Global scope array Counter
var a = "" 	// Global scope Array size
var s = "searching"; // Global scope array Search

	// Builds the array
function createArray() {

  
  aArray = [];
  var newSize =
  parseInt(document.getElementById('size').value);
  
  // Clear display
  clearOutputDisplay();
  
  // loop to set 100 random array values
  for (var i = 0; i < newSize; i++) {
    aArray[i] = Math.floor(Math.random() * 100 + 1);
    c++;
  }
  
  // Writes to the array
  writeArray();
  
  // Writes the number of steps taken
  //writeSteps();
}

function clearOutputDisplay(){
 
	o = "";
	document.getElementById("aCreate").innerHTML = "";
}

	// Writes to the array
function writeArray(){

 		// loop to set 100 random array values
  for (var i = 0; i < aArray.length; i++) {
    o += i + ' ..... ' + aArray[i] + "<br/>";
    c++;
  }
  document.getElementById("aCreate").innerHTML = o;
}

function writeSteps(){
		// Writes number of steps taken by adding current values after removing old values
	document.getElementById("steps").innerHTML = "";
  
  document.getElementById("steps").innerHTML = 'The time compexity for ths Algorithm is O(n)';
  // document.getElementById("steps").innerHTML = 'The time compexity O(n) of this algorithm is O(' + c + ')';
}

function insertIntoArray() {
	// Clear display
  clearOutputDisplay();
  c = '0';
  // Returns the numbered value of the location requested 
  var l = parseInt(document.getElementById('location').value);
  
  // Returns the current value at the requested location
  var v = parseInt(document.getElementById('insert').value);
  var newSize =
  parseInt(document.getElementById('size').value);
  
  // New variable assignment
  var newL = l;
  var newV = v;
  
  o = 'Location ' + l + ' was updated to ' + v + '' + '<br/>';
  
  for (l = newSize - 1; l > newL; l--) {
  	aArray[l] = aArray[l-1];
  c++;
  }
  
  aArray[newL] = v;
   //...