Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: what's the big-O time of declaring an array of size n?

What is the running time of declaring an array of size n in Java? I suppose this would depend on whether the memory is zero'ed out on garbage collection (in which case it could be O(1) ) or on initialization (in which case it'd have to be O(n) ).

like image 585
Mala Avatar asked Apr 12 '11 19:04

Mala


People also ask

What is the time complexity of array in Java?

The ArrayList in Java is backed by an array. So let's focus first on the time complexity of the common operations at a high level: add() – takes O(1) time; however, worst-case scenario, when a new array has to be created and all the elements copied to it, it's O(n) add(index, element) – on average runs in O(n) time.

What is the time complexity to get an element from an array?

Because it takes a single step to access an item of an array via its index, or add/remove an item at the end of an array, the complexity for accessing, pushing or popping a value in an array is O(1). Whereas, linearly searching through an array via its index, as seen before, has a complexity of O(n).

What is the time complexity of adding element at the beginning of ArrayList?

Adding an element to beginning of array is O(n) - it would require to shift all the existing elements by one position. All elements in an array list are stored in a contiguous array. If you add more elements than the current size of the array - it will be grown automatically to accommodate the new element.


2 Answers

It's O(n). Consider this simple program:

public class ArrayTest {    public static void main(String[] args) {      int[] var = new int[5];   }  } 

The bytecode generated is:

Compiled from "ArrayTest.java" public class ArrayTest extends java.lang.Object{ public ArrayTest();   Code:    0:   aload_0    1:   invokespecial   #1; //Method java/lang/Object."<init>":()V    4:   return  public static void main(java.lang.String[]);   Code:    0:   iconst_5    1:   newarray int    3:   astore_1    4:   return  } 

The instruction to take a look at is the newarray instruction (just search for newarray). From the VM Spec:

A new array whose components are of type atype and of length count is allocated from the garbage-collected heap. A reference arrayref to this new array object is pushed into the operand stack. Each of the elements of the new array is initialized to the default initial value for the type of the array (§2.5.1).

Since each element is being initialized, it would take O(n) time.

EDIT

Looking at the link amit provided, it is possible to implement array-initialization with a default value, in constant time. So I guess it ultimately depends on the JVM. You could do some rough benchmarking to see if this is the case.

like image 154
Vivin Paliath Avatar answered Sep 23 '22 06:09

Vivin Paliath


A small none professional benchmark on JRE1.6:

public static void main(String[] args) {     long start = System.nanoTime();     int[] x = new int[50];     long smallArray = System.nanoTime();     int[] m = new int[1000000];     long bigArray = System.nanoTime();     System.out.println("big:" +  new Long( bigArray - smallArray));     System.out.println("small:" +  new Long( smallArray - start));   } 

gave the following result:

big:6133612 small:6159 

so I assume O(n). of course, it is not enough to be sure, but it's a hint.

like image 31
amit Avatar answered Sep 24 '22 06:09

amit