Efficiency of Algorithms
Efficiency of Algorithms
Chapter's Topics:
1. Definition
2. Common Algorithms
3. Big-O Notation
#include <iostream>
int main(){
int i = 1, n;
Linear algorithm
number that controls the loop
number of times the loop is repeated
#include <iostream>
int main(){
int n;
cout << "Enter a value for n: ";
cin >> n;
cout << "\nLooping " << n << " times.\n" << endl;
for(int i = 1; i <= n; i++){
cout << "Value of n = " << n << endl;
cout << "Value of i = " << i << endl;
cout << endl;
}
return 0;
}
#include <iostream>
#include <iostream>
int main(){
int n;
cout << "Enter a value for n: ";
cin >> n;
for(int i = 1; i <= n; i++){
for(int j = i; j <= n; j++){
cout << endl;
cout << "n = " << n << endl;
cout << "i = " << i << endl;
cout << "j = " << j << endl;
}
}
return 0;
}
Quadratic algorithm
number that controls the loop
number of times the loop is repeated
#include <iostream>
int main(){
int a, b;
cout << "Enter a value for a and b: ";
cin >> a >> b;
for(int i = 1; i <= a; i++){
for (int j = 1; j < b; j++){
cout << "Value of a = " << a << endl;
cout << "Value of b = " << b << endl;
cout << "Value of i = " << i << endl;
cout << "Value of j = " << j << endl;
cout << endl;
}
}
return 0;
}
Exponential
#include <iostream>
int main(){
int input;
cout << "Enter a number to do the Fibonacci sequence on: ";
cin >> input;
cout << fibonacci(input);
return 0;
}