Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the efficiency of this program?

Tags:

java

arrays

big-o

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]);
       }
    }
}
like image 525
nope Avatar asked May 23 '11 16:05

nope


People also ask

What is the efficiency of a program?

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.

What is efficiency example?

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.

What are the 4 types of efficiency?

There are several types of efficiency, including allocative and productive efficiency, technical efficiency, 'X' efficiency, dynamic efficiency and social efficiency.


3 Answers

O (n*m) where n the number of arrays (first dimenstion) and m the max size of each internal array (second dimension)

like image 196
George Kastrinis Avatar answered Oct 17 '22 04:10

George Kastrinis


I'd even note that the size of m is comparable to the size of n and make this O(n2).

like image 26
Charlie Martin Avatar answered Oct 17 '22 06:10

Charlie Martin


Considering that your algorithm visits every element in the array once, it is O(n) where n is the size of the 2D array.

like image 36
matt b Avatar answered Oct 17 '22 06:10

matt b