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.

Answer from Niklas B. on Stack Overflow
🌐
Gregory Gaines
gregorygaines.com › blog › what-is-the-time-complexity-arrays-and-collections-sort
What Is the Time Complexity of Arrays.sort() and Collections.sort() | Gregory Gaines
March 12, 2022 - The time complexity including the comparator is O(n * (log n) * c), where c is the time complexity of the comparator. By default, Arrays.sort uses a comparator following the natural ordering of the content for an O(1) time complexity.
🌐
Baeldung
baeldung.com › home › java › java array › time comparison of arrays.sort(object[]) and arrays.sort(int[])
Time Comparison of Arrays.sort(Object[]) and Arrays.sort(int[]) | Baeldung
January 8, 2024 - That’s a great average sorting time for many collections to have. Moreover, it has the advantage of being completely in place, so it does not require any additional storage. Though, in the worst case, its time complexity is O(n2). So, which algorithm is faster and why? Let’s first do some theory, and then we’ll run some concrete tests with JMH. Arrays.sort(Object[]) is typically slower compared to Arrays.sort(int[]) for a few different reasons.
Discussions

performance - The Running Time For Arrays.Sort Method in Java - Stack Overflow
It depends on what you are sorting and on your Java version. Sort methods for arrays of different types have different time and space complexities. More on stackoverflow.com
🌐 stackoverflow.com
java - What is the time complexity of Arrays,sort(String []) - Stack Overflow
I know Arrays.sort(int[]) will have time complexity of O(nlgn). But what about string array. Since sorting string array uses compareTo function internally to compare two string. More on stackoverflow.com
🌐 stackoverflow.com
algorithm - What is the worst case time complexity of Java 14+ Arrays.sort( int[] )? - Stack Overflow
On the other hand since java 14 fail safe for higher complexity is changing sort algorithm to heapsort which have worst complexity of O(n log n) so it never will be slower than that. More on stackoverflow.com
🌐 stackoverflow.com
JAVA: Is Arrays.sort(array) more efficient than other sorts?
Body Just Discovered a new Inbuilt Method in JAVA Arrays.sort(arrays) IS IT EFFICIENT? Time Complexity? Guidelines I have read and understood this category's guidelines before making this post. More on github.com
🌐 github.com
2
1
May 25, 2024
Top answer
1 of 8
46

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.

2 of 8
11

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))

🌐
Medium
medium.com › @AlexanderObregon › javas-arrays-sort-explained-d7a2d48f1cc0
Java’s Arrays.sort() Explained | Medium
September 23, 2024 - This partitioning step is repeated ... entire array is sorted. Quicksort is a comparison sort, meaning it makes decisions based on comparing elements to one another. While its worst-case time complexity is O(n²) (when the pivot is poorly chosen, leading to imbalanced partitions), this can be mitigated by techniques like choosing a random pivot or using median-of-three pivot selection. Java’s Arrays.sort() ...
🌐
Stack Abuse
stackabuse.com › sorting-algorithms-in-java
Sorting Algorithms in Java
August 10, 2023 - Each of those n times we're iterating through the whole array (for-loop in the code), meaning the worst case time complexity would be O(n^2). Note: The time complexity would always be O(n^2) if it weren't for the sorted boolean check, which terminates the algorithm if there aren't any swaps ...
Find elsewhere
Top answer
1 of 1
16

The problem with saying: Sorting is O(n logn) is that this is an incomplete and therefore nebulous, undefined statement. It is unclear what it means. The problem is: You have failed to define what n is.

Now, in practice, n is usually obvious from context. For example, If I say: "Sorting an array of integers is O(n logn) assuming an optimal sorting algorithm", just about everybody who hears this will immediately realize that, whilst I didn't explicitly say what n is, clearly I mean: n = the number of ints in that array.

So let's go back to your problem: Now n is no longer clear.

Is n the # of strings in the array? Is n the average # of characters for all strings in that array?

We're really talking about 2 separate variables here. So, to properly describe the performance characteristics in terms of big-O notation for this problem, you'd say something like:

  • Given: String[] str, for example: str = {"Hello", "World", "John", "Doe"}
  • Let n be the number of elements in the str array.
  • Let m be the average # of characters for the strings in the str array
  • Then, calling Arrays.sort(str) on this array would have the performance characteristic of O(m * n log n).

The reason it's O(m*n logn) is because the sorting algorithm itself will run O(n logn) comparison operations in order to perform the sort. And each comparison operation takes O(m) time to finish, though that is very much the worst case scenario: Given, say, Hello and World, even though m = 5 for those two, the comparison completes after only 1 step; the algorithm compares H and W and answers immediately, never even looking at ello or orld. But if the strings are Hello1, Hello2, Hello3 and Hello4, then that's a guaranteed 5 'steps' for every time 2 strings are compared, and there will be ~n logn) comparisons. big-O is 'worst case scenario' (vs little o), hence, the algorithm is O(m*n log n).

Even though 'everybody says' O(n log n). Contex is important.

🌐
LinkedIn
linkedin.com › pulse › difference-btw-arrayssort-collectionssort-terala-chittibabu
Difference Btw Arrays.sort() and Collections.sort() ?
June 22, 2022 - It can be integer array, float array, String array, Array of objects etc. The time complexity for this method is O(nlogn) as it runs TimSort in background. TimSort algorithm makes use of the Insertion sort and the MergeSort algorithms. sort() ...
🌐
Baeldung
baeldung.com › home › java › java collections › time complexity of java collections sort in java
Time Complexity of Java Collections Sort in Java | Baeldung
April 22, 2025 - Timsort will perform around 60-70 comparisons and swaps for this particular array. Running the following codе will dеmonstratе thе еxеcution timе for sorting an unsortеd list, showcasing thе avеragе and worst-casе pеrformancе of ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-handling-tle-while-using-arrays-sort-function
Java | Handling TLE While Using Arrays.sort() Function - GeeksforGeeks
July 12, 2025 - It is because Arrays.sort() function uses Merge Sort for sorting object array, which has a worst-case complexity of O( ... N^2). Below java code shows the run time comparison between using Arrays.sort() function on the array of values with that ...
🌐
Dreamix
dreamix.eu › home › insights › tech › sorting an array in java: tips & tricks
Sorting An Array In Java: Tips & Tricks
July 16, 2024 - Under the hood of this sort() method ... non-primitive objects, making it faster and quite efficient as the time complexity is O(n log(n))....
🌐
Medium
medium.com › @reetesh043 › collection-sort-vs-arrays-sort-in-java-1528f83223c3
Collection.sort() vs Arrays.sort() in Java | by Reetesh Kumar
February 21, 2024 - Both methods offer efficient sorting with O(n log n) time complexity. Consider custom comparators and List implementation details for specific performance assessments. Both Collection.sort() and Arrays.sort() are essential tools for sorting ...
🌐
GitHub
github.com › orgs › community › discussions › 125677
JAVA: Is Arrays.sort(array) more efficient than other sorts? · community · Discussion #125677
May 25, 2024 - I expect the latter is true, seen ... I've not covered. My algorithm's time complexity (O(n^2)) and the native methods' (O(n log(n)))), which could obviously cause an impact....
🌐
LabEx
labex.io › tutorials › java-how-to-handle-array-sorting-complexity-514705
How to handle array sorting complexity | LabEx
graph TD A[Input Size] --> B{Sorting Algorithm} B --> C[Time Complexity] B --> D[Space Complexity] C --> E[Performance Characteristics] D --> E ... Mastering array sorting complexity in Java requires a deep understanding of different sorting algorithms, their time and space complexities, and strategic implementation.
🌐
Codeforces
codeforces.com › blog › entry › 46756
Java: Arrays.sort(Integer) vs Arrays.sort(int) with Examples - Codeforces
Instead, in the Arrays.sort(Object) case, it uses Mergesort, which has a O(n log n) worst case. So some problems may give you Time Limit Exceeded when you use Arrays.sort(int) if there is an anti-quicksort test.
🌐
Sentry
sentry.io › sentry answers › java › why is processing a sorted array faster than processing an unsorted array in java?
Sorted vs Unsorted Array Processing Speed in Java | Sentry
3 weeks ago - Understand why sorted arrays process faster than unsorted arrays in Java through binary search with O(log n) time complexity versus linear search at O(n)
🌐
arXiv
arxiv.org › html › 2311.07298v2
Energy and Time Complexity for Sorting Algorithms in Java
Counting Sort does not utilise comparison for sorting. Instead, it "counts" the frequency of occurrences of each element in the array (Cormen, 2022). By accessing only the array, Counting Sort has a linear time complexity.