Question
C++ (Big-O Notation) Please, explain each code with a separate full program using these functions. (explain clearly) O (n log n) Lineararithmic int NlogN (
C++ (Big-O Notation)
Please, explain each code with a separate full program using these functions. (explain clearly)
O (n log n) Lineararithmic
int NlogN ( const int a[] , int n)
{
int result = 0;
for ( int j=0 ; j < n ; ++j)
for ( int i=n ; i > 0 ; i /= 2 )
{
result += a[i -1];
}
return result ;
}
O(n^c) Polynomial / Algebraic
Where c>2
int p( const int a[] , int n)
{
int result = 0;
for ( int i=0 ; i < n ; ++i)
for ( int j=0 ; j < n ; ++j)
for ( int k=0 ; k < n ; ++k)
result += a[k];
return result ;
}
O (c^n) Exponential
Where c>1
O(2n ) denotes an algorithm whose growth doubles with each additon to the input data set. int exp(int n)
{
if (n <= 1)
return n;
return exp (n - 2) + exp(n - 1) ;
}
O(n!) Factorial
A classic example is solving the traveling salesman problem using a brute-force approach.
for ( int i = 1; i <= factorial (n) ; ++i )
{
cout << "Hey - Im busy looking at: " + i) ;
}
If n is 8, this algorithm will run 8! = 40320 times!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started