Skip to content

Commit e4b6323

Browse files
authored
Merge pull request loiane#69 from loiane/third-edition
Third edition
2 parents 4fb0158 + 66b77eb commit e4b6323

File tree

166 files changed

+203
-77
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

166 files changed

+203
-77
lines changed

README.md

+13-13
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="05-BigONotation.js"></script>
9+
</body>
10+
</html>

examples/chapter15/01-BigONotation.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//*************** o(1)
2+
function increment(num){
3+
console.log('cost for increment with input ' + num + ' is 1');
4+
return ++num;
5+
}
6+
7+
increment(1);
8+
increment(2);
9+
10+
//*************** o(n)
11+
12+
function createNonSortedArray(size){
13+
var array = [];
14+
15+
for (var i = size; i> 0; i--){
16+
array[i] = i;
17+
}
18+
19+
return array;
20+
}
21+
22+
function sequentialSearch(array, item){
23+
var cost = 0;
24+
for (var i=0; i<array.length; i++){
25+
cost++;
26+
if (item === array[i]){ //{1}
27+
return i;
28+
}
29+
}
30+
console.log('cost for sequentialSearch with input size ' + array.length + ' is ' + cost);
31+
return -1;
32+
}
33+
34+
var array = createNonSortedArray(99);
35+
sequentialSearch(array, -1);
36+
37+
//*************** o(nˆ2)
38+
39+
function swap(array, index1, index2){
40+
var aux = array[index1];
41+
array[index1] = array[index2];
42+
array[index2] = aux;
43+
}
44+
45+
function bubbleSort(array){
46+
var length = array.length;
47+
var cost = 0;
48+
for (var i=0; i<length; i++){ //{1}
49+
cost++;
50+
for (var j=0; j<length-1; j++ ){ //{2}
51+
cost++;
52+
if (array[j] > array[j+1]){
53+
swap(array, j, j+1);
54+
}
55+
}
56+
}
57+
console.log('cost for bubbleSort with input size ' + length + ' is ' + cost);
58+
}
59+
60+
var array1 = createNonSortedArray(99);
61+
var array2 = createNonSortedArray(999);
62+
var array3 = createNonSortedArray(9999);
63+
bubbleSort(array1);
64+
bubbleSort(array2);
65+
bubbleSort(array3);

examples/chapter15/bigOChart/chart.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
google.load('visualization', '1.0', {'packages':['corechart']});
2+
google.setOnLoadCallback(drawChart);
3+
4+
function drawChart() {
5+
6+
var 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+
15+
for (var i = 0; i <= 30; i++) {
16+
data.addRow([i+'', 1, Math.log(i), i, Math.log(i)*i, Math.pow(i,2), Math.pow(2,i)]);
17+
}
18+
19+
var options = {'title':'Big O Notation Complexity Chart',
20+
'width':700,
21+
'height':600,
22+
'backgroundColor':{stroke:'#CCC',strokeWidth:1},
23+
'curveType':'function',
24+
'hAxis':{
25+
title:'Elements (n)',
26+
showTextEvery:5
27+
},
28+
'vAxis':{
29+
title:'Operations',
30+
viewWindowMode:'explicit',
31+
viewWindow:{min:0,max:450}
32+
}
33+
};
34+
35+
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
36+
chart.draw(data, options);
37+
}
+14
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="chart.js"></script>
10+
</head>
11+
<body>
12+
<div id='chart_div'></div>
13+
</body>
14+
</html>

0 commit comments

Comments
 (0)