Java – add arrays. Java Sort() increases time complexity and space-time complexity?

There are array related problems, which require that the time complexity is O (n) and the space complexity is O (1)

If I use arrays Sort (ARR) and use a for loop for a loop, for example:

public static int hello(int[]A){
  Arrays.sort(A);
  for(int i=0;i<A.length;i++){
     ....................
  }
  return ....;

}

So the loop will take o (n) time My question is: put arrays Sort() takes more time? If I use arrays Sort(), the time complexity is still o (n)? And arrays Does sort () take more space?

Solution

I suppose you're talking about Java here

Yes, arrays Sort (int []) is a sort based on comparison in all Java standard library implementations I know, so it is an example of must have worst case complexity Ω (n log n) In particular, Oracle Java 7 uses a two axis quick sort variant for integer overloading, which actually has an Ω N2) worst case

Likely to use ω (1) Space (which means that another is that space use is not o (1)) Although it is impossible to achieve comparison based sorting, there is only constant additional space, which is very impractical

That is, under certain conditions, specific types of data can be sorted in linear time, such as:

> http://en.wikipedia.org/wiki/Counting_sort > http://en.wikipedia.org/wiki/Pigeonhole_sort > http://en.wikipedia.org/wiki/Radix_sort

Using a constant range of input integers (for example, for some constants C, ABS (a [i]) < = C), count sorting and cardinality sorting use only O (n) time and O (1) space, so this may be useful

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>