Skip to content

Instantly share code, notes, and snippets.

@elyager
Last active September 28, 2018 21:34
Show Gist options
  • Save elyager/46a6e3434c7e317bd0a77c298bac57b6 to your computer and use it in GitHub Desktop.
Save elyager/46a6e3434c7e317bd0a77c298bac57b6 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>BigO Notation</title>
</head>
<body>
<h1>Big O Notation</h1>
<script>
//O(1)
function constantTime(input) {
console.log(input)
}
//O(n)
function linearTime(input) {
for (let i = 0; i < input; i++) {
console.log(i)
}
}
//O(log n)
function logarithmicTime(input) {
for (let j = input; j > 0; j = Math.floor(j / 2)) {
console.log(j)
}
}
// O(n^2)
function cuadraticTime(inputA, inputB) {
for (let i = 0; i < inputA; i++) {
for (let j = 0; j < inputB; j++) {
console.log(j)
}
}
}
// O(n^3)
function cubicTime(inputA, inputB, inputC) {
for (let i = 0; i < inputA; i++) {
console.count(i)
for (let j = 0; j < inputB; j++) {
for (let h = 0; h < inputC; h++) {
console.log(h)
}
}
}
}
//O(n!)
function factorialTime(input) {
let times = factorialHelper(input)
for (let i = 0; i < times; i++) {
console.log('operations')
}
}
//O(n log n)
function linearLogTime(input) {
for (let i = 0; i < input; i++) { //O(n)
for (let j = input; j > 0; j = Math.floor(j / 2)) { //O(log n)
console.log(j)
}
}
}
//Helpers
function factorialHelper(input) {
if (input == 0) {
return 1
}
return input * factorialHelper(input - 1)
}
function printWithTime(fn, input, title) {
console.time(title)
fn.apply(this, input)
console.timeEnd(title)
}
console.log('\n###### CONSTANT TIME ########')
printWithTime(constantTime, [10], 'Constant Time')
printWithTime(constantTime, [100000], 'Constant Time')
printWithTime(constantTime, [1000000], 'Constant Time')
console.log('\n###### LINEAR TIME ########')
printWithTime(linearTime, [10], 'Linear Time 10')
printWithTime(linearTime, [100], 'Linear Time 100')
printWithTime(linearTime, [1000], 'Linear Time 1000')
console.log('\n###### logarithmic TIME ########')
printWithTime(logarithmicTime, [10], 'logarithmic Time')
printWithTime(logarithmicTime, [1000], 'logarithmic Time')
console.log('\n###### CUADRATIC TIME ########')
printWithTime(cuadraticTime, [1, 10], 'Cuadratic Time')
printWithTime(cuadraticTime, [10, 100], 'Cuadratic Time')
console.log('\n###### CUBIC TIME ########')
printWithTime(cubicTime, [1, 10, 100], 'Cubic Time')
printWithTime(cubicTime, [1, 100, 1000], 'Cubic Time')
console.log('\n###### FACTORIAL TIME ########')
printWithTime(factorialTime, [2], 'Factorial Time 2')
printWithTime(factorialTime, [6], 'Factorial Time 6')
console.log('\n###### LINEAR TIME + logarithmic TIME ########')
printWithTime(logarithmicTime, [1000], 'Linear logarithmic Time')
printWithTime(linearLogTime, [10], 'Linear logarithmic Time')
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment