I am assuming you are talking about Java here.
So the loop will cost O(n) time, my question is that will Arrays.sort() cost more time?
Yes, Arrays.sort(int[]) in all Java standard library implementations that I know, is an example of a comparison-based sort and thus must have worst-case complexity Ω(n log n). In particular, Oracle Java 7 uses a dual-pivot quicksort variant for the integer overloads, which actually has an Ω(n2) worst case.
and will Arrays.sort() cost more space?
In all likelihood it will use ω(1) space (which means another yes, the space usage is not O(1)). While it's not impossible to implement a comparison-based sort with only constant extra space, it's highly impractical.
That said, under certain conditions it is possible to sort specific types of data in linear time, see for example:
- http://en.wikipedia.org/wiki/Counting_sort
- http://en.wikipedia.org/wiki/Pigeonhole_sort
- http://en.wikipedia.org/wiki/Radix_sort
With a constant range of input integers (for example if abs(A[i]) <= C for some constant C), then counting sort and radix sort use indeed only O(n) time and O(1) space, so that might be useful.
performance - The Running Time For Arrays.Sort Method in Java - Stack Overflow
java - What is the time complexity of Arrays,sort(String []) - Stack Overflow
algorithm - What is the worst case time complexity of Java 14+ Arrays.sort( int[] )? - Stack Overflow
JAVA: Is Arrays.sort(array) more efficient than other sorts?
I am assuming you are talking about Java here.
So the loop will cost O(n) time, my question is that will Arrays.sort() cost more time?
Yes, Arrays.sort(int[]) in all Java standard library implementations that I know, is an example of a comparison-based sort and thus must have worst-case complexity Ω(n log n). In particular, Oracle Java 7 uses a dual-pivot quicksort variant for the integer overloads, which actually has an Ω(n2) worst case.
and will Arrays.sort() cost more space?
In all likelihood it will use ω(1) space (which means another yes, the space usage is not O(1)). While it's not impossible to implement a comparison-based sort with only constant extra space, it's highly impractical.
That said, under certain conditions it is possible to sort specific types of data in linear time, see for example:
- http://en.wikipedia.org/wiki/Counting_sort
- http://en.wikipedia.org/wiki/Pigeonhole_sort
- http://en.wikipedia.org/wiki/Radix_sort
With a constant range of input integers (for example if abs(A[i]) <= C for some constant C), then counting sort and radix sort use indeed only O(n) time and O(1) space, so that might be useful.
According to the java jvm 8 javadocs of Arrays.sort() method:
The sorting algorithm is a Dual-Pivot Quicksort by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm offers O(n log(n)) performance on many data sets that cause other quicksorts to degrade to quadratic performance, and is typically faster than traditional (one-pivot) Quicksort implementations.
So it will increase your time complexity from O(n) to O(n log(n))
From official docs
I've observed that there are primarily two approaches. So, it depends on what you are sorting and what overloaded method from sort family of methods you are calling.
Docs mention that for primitive types such as long, byte (Ex: static void sort(long[])):
The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.
For Object types: (Ex: void sort(Object list[]))
Guaranteed O(nlogn) performance
The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n*log(n) performance.
Hope that helps!
Arrays.sort() uses Tim sort - O(N log N) for array of objects and QuickSort for arrays of primitives - again O(N log N).
Here is an awesome comparison of sorting algorithms: http://www.sorting-algorithms.com/
An array that is already sorted is often, but not necessarily, the best case for a given sorting algorithm. For instance, if you choose your pivot point properly, a sorted array could actually be the worst case for quicksort.
Sorted arrays can equal worst complexity in a few cases.
BubbleSort will give best performance for a sorted array.
HeapSort will give worse performance for a sorted array if you are chaging the sort order ,for ex if you are sorting an array in acesindg order and the inpur array is in descending order
Insertion sort gives great performance on sorted arrays or nearly sorted arrays
Selection sort is O(n^2) irrespective of what array you provide it as an input :)
Regarding your second question, most of the time complexity is always the worst case time complexity. For ex Insertion sort is O(n^2) but works really fast for almost sorted arrays. There are a few sorting algorithms which work great for less than 1000 elements but go into exponential complexity for higher number of elements