Friday, September 28, 2018

How we can decide which algrothim is best ?

For example we have multiple algorithms for same task or same propose then how we can decide which is best algorithm is best.
Task: i want to print London five time so i have multiple algorithms to do this.So i will pick best algorithm for this.First we will think about the property of best algorithm.there are multiple properties of best algorithms.
  1. Memory--the algorithm which take less memory
  2. Time--the algorithm which take minimum time during execution and writing.
There are multiple method to print London five time

1

void print()
{
for(int i=1;i<=5;i++)
    cout<<"London;
}

2

void print()
{
    cout<<"London;
    cout<<"London;
    cout<<"London;
    cout<<"London;
    cout<<"London;
}

Here 1 method use for loop and use print instruction only one time and print london five times.
Where in method 2 print instruction use five time to print london five time.so you can see 2 method will take more time to write and take more space in memory and also take more time to execute .
so we can say method 1 is the best case in term of  Memory and Time.

No comments:

Post a Comment