What's the efficiency (in Big O notation) of a simple program that traverses a 2D array of ints and outputs each element.
Take the following code as an example:
public static void main(String args[])
{
int[] array = {{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}};
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
System.out.println(array[i][j]);
}
}
}
Program Efficiency relates to the cost of producing products or services relative to other programs or to some ideal process. Cost Effectiveness relates to an analysis of the costs – money, people, time, materials, etc.
Examples of efficiency in a SentenceBecause of her efficiency, we got all the work done in a few hours. The factory was operating at peak efficiency. A furnace with 80 percent fuel efficiency wastes 20 percent of its fuel. The company is trying to lower costs and improve efficiencies.
There are several types of efficiency, including allocative and productive efficiency, technical efficiency, 'X' efficiency, dynamic efficiency and social efficiency.
O (n*m) where n the number of arrays (first dimenstion) and m the max size of each internal array (second dimension)
I'd even note that the size of m is comparable to the size of n and make this O(n2).
Considering that your algorithm visits every element in the array once, it is O(n)
where n
is the size of the 2D array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With