var array = new Array(1000);
var d = "";
var count = 0;
var count2 = 0;
var found = 0;
for (var i = 0; i < array.length; i++) {
array[i] = Math.floor(Math.random() * 100 + 1)
}
function showArray() {
for (var i = 0; i < array.length - 1; i++) {
d += i + ' : ' + array[i] + "<br/>";
}
document.getElementById("output").innerHTML = "Insertion Operation Count : " + count + "<br/>" + "Comparison Operation Count : " + count2 + "<br/>" + d;
}
function hideArray() {
d = "";
document.getElementById("output").innerHTML = " ";
}
showArray();
// Big O notation for this function is O(n). The number of operations that occurs is directly increased by n
function insertIntoArray() {
hideArray();
var index = parseInt(document.getElementById('index').value);
var value = parseInt(document.getElementById('value').value);
d = 'Inserting ' + value + ' at index : ' + index + '<br/>';
for (var i = array.length - 1; i > index; i--) {
count++;
array[i] = array[i - 1];
}
array[index] = value;
count++;
showArray();
}
// Big O for this function is O(n) Since it is a linear comparison of the entire array.
function searchArray() {
var search = parseInt(document.getElementById("search").value);
for (var i = 0; i < array.length; i++) {
count2++;
if (array[i] == search) {
found++;
}
}
d = found + ' Results found in array' + '<br/>';
showArray();
}
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.