如何找到我的算法的最佳情况和最坏情况的公式? [英] How to find the formula of best case and worst case of my algorithm?

查看:134
本文介绍了如何找到我的算法的最佳情况和最坏情况的公式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被给了一个任务。编写一个算法,以便输入2个数据列表,至少有一个共同点。

所以,这是我的算法:(我将代码写入php)

  $ arrayA = array('5','6','1','2','7' ); 
$ arrayB = array('9','2','1','8','3');
$ arrayC = array();
$ b foreach($ arrayA as $ val){
if(in_array($ val,$ arrayB)){
array_push($ arrayC,$ val);




$ b

这就是我自己的算法,不确定它是否合适一。那么,基于我的算法,如何找到最佳情况和最坏情况(大O)的公式?注意:请让我知道,如果我的算法是错误。我的目标是输入2个数据列表,至少有一个共同点。

解决方案

为了让你开始:


  1. 你有任何显式循环吗?如果是这样,他们将运行多少次(max,min,avg)?

  2. 您是否有任何隐式循环?如果是这样,他们将运行多少次(max,min,avg)?

  3. 这些循环中的任何一个是否嵌套?如果是这样,他们是否依赖对方?它们以何种方式依赖于


I was given a task. Write an algorithm so that, the input of 2 lists of data, will have at least one in common.

So, this is my algorithm: (I write the 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);
    }
}

Thats my own algo, not sure if its a good one. So, based on my algorithm, how to find the formula of best case and worst case (big O)?

Note: Please do let me know, if my algorithm is wrong. My goal is " input of 2 lists of data, will have at least one in common."

解决方案

To get you started:

  1. Do you have any explicit loops? If so, how many times will they run (max, min, avg)?
  2. Do you have any implicit loops? If so, how many times will they run (max, min, avg)?
  3. Are any of these loops nested? If so, do they depend on each other? In what ways are they dependent

这篇关于如何找到我的算法的最佳情况和最坏情况的公式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆