algorithm - What is the asymptotic time complexity of the following piece of code? -
how come big o notation?
float sum = 0 ; ( int = 1; < n ; i++) { sum + = a[i]; } cout << sum;
great asked this. went on in class , asked same question. big o notation used describe how efficient or complex algorithm is.
o(1) algorithm execute in same time. efficient type of algorithm. example
bool bigo(string[] big) { if(big[0] == null) { return false; } return true; }
there o(n) depend on size of input. example
bool bigo(string[] strings, string value) { for(int = 0; < strings.length; i++) { if(strings[i] == value) { return true; } } return false; }
as can tell method can take longer execute depending on input. if strings.length small quick if large length take while.
and there o(n^2). involves multiple loops within self. can o(n^3) depending on how deep nested iterations. example
bool bigo(string[] strings) { for(int = 0; < strings.length; i++) { for(int j = 0; j < strings.length; j++) { if(i == j) { continue; } if(strings[i] == strings[j]) { return true; } } } return false; }
now looking @ algorithm think yours is? if said o(n) you're right. big o notation dependent on how efficient algorithm is. efficiency can depend on cpu (time) usage, ,memory usage ,disk usage , network usage
Comments
Post a Comment