Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8652345

Browse files
committed
Add Big O notation examples in src/02-bigOnotation/01-big-o-intro.js, 02-bigOChart.html, and 02-bigOChart.js
1 parent 51c93b4 commit 8652345

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Path: src/02-bigOnotation/01-big-o-intro.js
2+
3+
// O(1) - Constant Time
4+
function multiplyBy3(num) {
5+
console.log(`${num} * 3 = `, num * 3);
6+
}
7+
8+
console.log('O(1) - Constant Time');
9+
multiplyBy3(5);
10+
multiplyBy3(50);
11+
12+
// O(n) - Linear Time
13+
function multiplicationTable(num, x) {
14+
for (let i = 1; i <= x; i++) {
15+
console.log(`${num} * ${i} = `, num * i);
16+
}
17+
}
18+
19+
console.log('*******************');
20+
console.log('O(n) - Linear Time');
21+
console.log('Multiplication table for 5 with x = 3');
22+
multiplicationTable(5, 3);
23+
24+
console.log('Multiplication table for 5 with x = 10');
25+
multiplicationTable(5, 10);
26+
27+
// O(n^2) - Quadratic Time
28+
function multiplicationTable2(num, x) {
29+
for (let i = 1; i <= num; i++) {
30+
console.log(`Multiplication table for ${i} with x = ${x}`);
31+
for (let j = 1; j <= x; j++) {
32+
console.log(`${i} * ${j} = `, i * j);
33+
}
34+
}
35+
}
36+
37+
console.log('************************');
38+
console.log('O(n^2) - Quadratic Time');
39+
multiplicationTable2(3, 2);
40+
41+
// O(nˆ3) - Cubic Time
42+
function multiplicationTable3(num, x) {
43+
for (let i = 1; i <= num; i++) {
44+
for (let j = 1; j <= x; j++) {
45+
for (let k = 1; k <= x; k++) {
46+
console.log(`${i} * ${j} * ${k} = `, i * j * k);
47+
}
48+
}
49+
}
50+
}
51+
52+
console.log('************************');
53+
console.log('O(n^3) - Cubic Time');
54+
multiplicationTable3(2, 3);
55+
56+
// calculating the time complexity of the function
57+
function multiplicationTableRefactored(num, x) {
58+
59+
let s = ''; // {1}
60+
let numberOfAsterisks = num * x; // {2}
61+
for (let i = 1; i <= numberOfAsterisks; i++) { // {3}
62+
s += '*';
63+
}
64+
console.log(s); // {4}
65+
console.log('Calculating the time complexity of a function'); // {5}
66+
67+
for (let i = 1; i <= num; i++) { // {6}
68+
console.log(`Multiplication table for ${i} with x = ${x}`); // {7}
69+
for (let j = 1; j <= x; j++) { // {8}
70+
console.log(`${i} * ${j} = `, i * j); // {9}
71+
}
72+
}
73+
}
74+
75+
multiplicationTableRefactored(3, 2);
76+
// to see the output of this file use the command: node src/02-bigOnotation/01-big-o-intro.js
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<style>
6+
body { background-color: #DDDDDD; font: 30px sans-serif; }
7+
</style>
8+
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
9+
<script type="text/javascript" src="02-bigOChart.js"></script>
10+
</head>
11+
<body>
12+
<div id='chart_div'></div>
13+
</body>
14+
</html>

‎src/02-bigOnotation/02-bigOChart.js‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
google.load('visualization', '1.0', {'packages':['corechart']});
2+
google.setOnLoadCallback(drawChart);
3+
4+
function drawChart() {
5+
6+
const data = new google.visualization.DataTable();
7+
data.addColumn('string', 'n');
8+
data.addColumn('number', 'O(1)');
9+
data.addColumn('number', 'O(log n)');
10+
data.addColumn('number', 'O(n)');
11+
data.addColumn('number', 'O(n log n)');
12+
data.addColumn('number', 'O(n^2)');
13+
data.addColumn('number', 'O(2^n)');
14+
//data.addColumn('number', 'O(n!)');
15+
16+
for (let i = 0; i <= 30; i++) {
17+
data.addRow([i+'', 1, Math.log(i), i, Math.log(i)*i, Math.pow(i,2), Math.pow(2,i)]);
18+
}
19+
20+
const options = {
21+
'width':700,
22+
'height':600,
23+
'backgroundColor':{stroke:'#CCC',strokeWidth:1},
24+
'curveType':'function',
25+
'legend': { position: 'right'},
26+
'hAxis':{
27+
title:'Elements (n)',
28+
showTextEvery:5
29+
},
30+
'vAxis':{
31+
title:'Operations',
32+
viewWindowMode:'explicit',
33+
viewWindow:{min:0,max:300}
34+
}
35+
};
36+
37+
const chart = new google.visualization.LineChart(document.getElementById('chart_div'));
38+
chart.draw(data, options);
39+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /