简体   繁体   English

对于 java 中的 3x3 多维数组,大 O 表示法是什么?

[英]What would the big O notation be for a 3x3 multidimensional array in java?

I am having trouble figuring out how to calculate the big O notation for a project.我无法弄清楚如何计算项目的大 O 表示法。 What would the big O notation be for a 3x3 grid, or a nxn grid with n being decided by the user.对于 3x3 网格或 n 由用户决定的 nxn 网格,大 O 表示法是什么?

Also, if I output a 3x3 grid multiple times, do I add the big O notations of each array together, or is there just one big O notation for all?另外,如果我多次 output 一个 3x3 网格,我是将每个数组的大 O 符号加在一起,还是只有一个大 O 符号?

Sorry, I'm a little confused on how big O notation works and how to calculate it, and what parts of code it can be used for.抱歉,我对大 O 表示法的工作原理、计算方法以及它可以用于代码的哪些部分有些困惑。

I really don't know what to do, this is the code for my multidimentional array.我真的不知道该怎么办,这是我的多维数组的代码。

String board[][] = new String[3][3];
    for (int i = 0; i < board.length; i++) {

        for (int j = 0; j < board[0].length; j++) {

            board[i][j] = "-";
        }
    }

The algorithm that you made is initializing a multidimensional array.您制定的算法正在初始化一个多维数组。 Although the array is filled by 9 of "-" , the Big O Notation implies the upper bound on the complexity of the algorithm which is the maximum number of operations that the algorithm will perform given the worst-case input.尽管数组由 9 个"-"填充,但大 O 表示法暗示了算法复杂度的上限,即在给定最坏情况输入的情况下算法将执行的最大操作数。 So, the input value that you have given (3x3) should not be considered to get the Big O Notation of your algorithms.因此,不应将您提供的输入值 (3x3) 视为获得算法的大 O 表示法。

Since you have two iterations in your algorithms, the maximum iteration would be n*n, so your Big O is O(n^2) .由于您的算法中有两次迭代,因此最大迭代将为 n*n,因此您的大 O 为O(n^2)

Because your input data is given, which are 3 and 3, you can estimate the time complexity via O(n^2) , which is O(9) .因为您的输入数据是给定的,分别是 3 和 3,所以您可以通过O(n^2)估算时间复杂度,即O(9)

For more information: https://www.geeksforgeeks.org/difference-between-big-oh-big-omega-and-big-theta/更多信息: https://www.geeksforgeeks.org/difference-between-big-oh-big-omega-and-big-theta/

Time complexity describes how an algorithm's time grows, as a function of the input, as time approaches infinity.时间复杂度描述了算法的时间如何随着时间接近无穷大而增长,如输入的 function。

In this case, it is clear that the running time depends only on the size of the input, not on its content.在这种情况下,很明显运行时间只取决于输入的大小,而不是它的内容。 So it is up to you to define a variable that describes your input size.因此,由您定义一个描述输入大小的变量。

In your code example, the input size is constant 3x3, so if this never changes, then you can just say the algorithm has constant time complexity, O(1) .在您的代码示例中,输入大小为常数 3x3,因此如果这永远不会改变,那么您可以说该算法具有常数时间复杂度O(1)

If the input can change, then it is up to you to decide how it can change, and how it makes sense to define it.如果输入可以改变,那么由你来决定它如何改变,以及定义它的意义。 For example, you can say that it is an n * n quadratic grid.例如,你可以说它是一个n * n的二次网格。 Fine.美好的。 Then, clearly both your loops run n times, so you get a total complexity of O(n * n) = O(n 2 ) .然后,很明显你的两个循环都运行了n次,所以你得到的总复杂度为O(n * n) = O(n 2 )

It is also perfectly correct to define n as the total number of elements in your array.n定义为数组中元素的总数也是完全正确的。 Then, we have some n = a * b .然后,我们有一些n = a * b One of your loops runs a times, the other b times, so in total the complexity becomes O(a * b) = O(n) .您的一个循环运行a次,另一个循环运行b次,因此总的来说复杂性变为O(a * b) = O(n)

As for your second question, yes if you apply this for a board m times, then your complexity would be O(mn 2 ) or O(mn) , depending on your chosen definition.至于你的第二个问题,是的,如果你将它应用于一块板m次,那么你的复杂性将是O(mn 2 )O(mn) ,具体取决于你选择的定义。

What matters is that you make your definitions clear and define your variables in a way that makes sense for what you are trying to say.重要的是你要明确你的定义,并以一种对你想说的有意义的方式定义你的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM