How to find the best case and worst case formula of my algorithm?

I was tasked. Write the algorithm so that the input of 2 data lists would have at least one common.

So this is my algorithm: (I am writing code in php)

$arrayA = array('5', '6', '1', '2', '7');
$arrayB = array('9', '2', '1', '8', '3');
$arrayC = array();

foreach($arrayA as $val){
    if(in_array($val, $arrayB)){
        array_push($arrayC, $val);
    }
}

This is my own algo, not sure if he is good. So, based on my algorithm, how to find the best case and worst case (large O) formula?

Note. Please let me know if my algorithm is incorrect. My goal is "entering two lists of data will have at least one thing in common."

+1
source share
3 answers

To get started:

  • Do you have any explicit loops? If so, how many times will they be launched (max, min, avg)?
  • - ? , (max, min, avg)?
  • - ? , ?
+1

, , , - : ( in_array , , O(m): ).

, n , O(n*m), n - , m .

, , , . , .

0

"" . , O (1) . , :

  • ,

, , , , , - , , - , .

, (@FrustratedWithFormsDesign allready ).

0

Source: https://habr.com/ru/post/1741777/


All Articles