Big O Calculator

Big O Calculator

Calculate and visualize the time complexity of algorithms with Big O notation.

Constant (O(1)) Linear (O(n)) Quadratic (O(n²)) Logarithmic (O(log n)) Exponential (O(2^n))

Not calculated yet.

let bigOSteps = “”; function calculateBigO(){ bigOSteps=””; const n = parseInt(document.getElementById(“algorithmInput”).value); const time = parseFloat(document.getElementById(“executionTime”).value); const algorithmType = document.getElementById(“algorithmType”).value; if(isNaN(n) || isNaN(time) || n <= 0 || time <= 0){ alert("Please enter valid positive values for all fields."); return; } let complexity = ""; switch(algorithmType) { case "constant": complexity = "O(1)"; break; case "linear": complexity = "O(n)"; break; case "quadratic": complexity = "O(n²)"; break; case "logarithmic": complexity = "O(log n)"; break; case "exponential": complexity = "O(2^n)"; break; } document.getElementById("bigOResult").value = complexity; bigOSteps += `Inputs:
Input Size: ${n}
Execution Time: ${time} ms

`; bigOSteps += `Selected Algorithm:
${complexity}

`; bigOSteps += `Explanation:
Big O Notation describes the upper bound of an algorithm’s growth rate based on the input size.

`; bigOSteps += `Tip: The higher the complexity, the more time it takes as the input grows. Exponential complexity grows very quickly as n increases.`; if(document.getElementById(“calculationStepsBigO”).style.display === “block”){ document.getElementById(“calculationStepsBigO”).innerHTML = bigOSteps; } } function resetBigO(){ document.querySelectorAll(“#calculator input”).forEach(el => el.value = “”); bigOSteps = “”; document.getElementById(“calculationStepsBigO”).innerHTML = “

Not calculated yet.

“; } function toggleBigOSteps(){ const s = document.getElementById(“calculationStepsBigO”); const a = document.getElementById(“toggleArrowBigO”); if(s.style.display === “none” || s.style.display === “”){ s.style.display = “block”; a.style.transform = “rotate(180deg)”; s.innerHTML = bigOSteps || “

Not calculated yet.

“; } else { s.style.display = “none”; a.style.transform = “rotate(0deg)”; } }

Leave a Reply

Your email address will not be published. Required fields are marked *