Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time complexity of System.arraycopy(...)?

System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length) is a native method.

What is the time complexity for this method?

like image 513
Kowser Avatar asked Aug 23 '11 18:08

Kowser


People also ask

What is the time complexity of system Arraycopy?

It's native , written in the language of the operating system, which means that the implementation of arraycopy() is platform dependant. So, in conclusion it's likely O(n), but maybe not. @HawkeyeParker, you can get the source code for a native method on a specific platform.

What is System Arraycopy () method?

arraycopy() method copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest.

Does System Arraycopy create new array?

arraycopy() simply copies values from the source array to the destination, Arrays. copyOf() also creates new array. If necessary, it will truncate or pad the content.

Is system Arraycopy deep copy?

System. arraycopy does shallow copy, which means it copies Object references when applied to non primitive arrays. Therefore after System.


1 Answers

It will have to go through all the elements in the array to do this. Array is a unique data structure where you have to specify a size when you initialize it. Order would be the source array's size or in Big O terms its O(length).

Infact this happens internally in an ArrayList. ArrayList wraps an array. Although ArrayList looks like a dynamically growing collection, internally it does an arrycopy when it has to expand.

like image 161
bragboy Avatar answered Oct 03 '22 02:10

bragboy