Assignment2
by Kristy Bond
HTML
<html>
<body>
<h1><center>
Time Complexity </center>
</h1> Array Size:
<input type="number" id="arraySize" value='100'>
<input type="button" value="Create Array" onclick="CreateArray()">
<br> Insert Value:
<input type="number" id="insertValue" value="">
<br>Insert Index:
<input type="number" id="insertIndex" value="">
<input type="button" onclick="InsertIntoArray()" value="Insert Into Array" id="InsertId">
<br/> Search Value:
<input type="number" id="search" value="">
<input type="button" onclick="SearchValue()" value="Search for Value">
<div id="output2">
</div>
<div id="output">
</div>
</body>
</html>
CSS
body {
background-color: lightblue;
}
h1 {
color: tan;
text-align: center;
font-family: arial;
font-size: 30px;
}
p {
text-align: center;
font-family: arial;
font-size: 15px;
}
JavaScript
var Array = [];
var out = "";
function ClearArray() {
out = "";
document.getElementById("output").innerHTML = "";
}
function DisplayArray() {
for (var i = 0; i < Array.length; i++) {
out += i + " : " + Array[i] + "<br>";
}
document.getElementById("output").innerHTML = out;
}
function CreateArray() {
ClearArray();
var length = document.getElementById('arraySize').value;
for (var i = 0; i < length; i++) {
Array[i] = Math.floor(Math.random() *
100 + 1);
}
DisplayArray();
}
function InsertIntoArray() {
ClearArray();
var size = document.getElementById("arraySize").value;
var index = document.getElementById("insertIndex").value;
var value = document.getElementById("insertValue").value;
var opperation = 0;
for (i = size - 1; i >= index; i--) {
if (i > index) {
Array[i] = Array[i - 1];
opperation++;
} else if (i == index) {
Array[i] = value;
opperation++;
}
}
document.getElementById('output2').innerHTML = "The value " + value + " has been inserted into index " + index + ". " + opperation + " operations were performed. Time complexity of O(" + opperation + ").";
DisplayArray();
}
function SearchValue() {
var search = document.getElementById('search').value;
var match = 0;
var firstIndex = null;
var opperation = 0;
for (var i = 0; i < Array.length; i++) {
if (Array[i] == search) {
match++;
}
}
for (var i = 0; i < Array.length; i++) {
opperation++;
if (Array[i] == search) {
firstIndex = i;
break;
}
}
if (firstIndex != null) {
document.getElementById("output2").innerHTML = "The Number " + search + " was found " + match + " times. There were " + opperation + " comparison operations done during the search. Time complexity of O(" + opperation + ").";
} else {
document.getElementById("output2").innerHTML = "Sorry, the number " + search + " was not found. There were " + opperation + " comparison operations done...