What Is the Time Complexity of Arrays.sort() and Collections.sort()
The interviewer asking the time complexity of Java's sorting algorithms stumped me. Top companies expect engineers to understand sorting and its use cases.
Table of Contents
Knowing time complexities are crucial for every software engineer, especially those who want to work at the top tech companies. The time complexity of sorting algorithms is especially important since their the basis for many other algorithms. This article explores the time complexity of Arrays.sort
and Collections.sort
, so you won't be surprised during your interview like I was.
Same Logic Under the Hood
Collections.sort
works on List
s whilst Arrays.sort
works on arrays. Inspecting the source reveals that Collections.sort
converts the passed List
into an array. After the conversion, Arrays.sort
is called on the newly allocated array.
Collections.sort1default void sort(Comparator << ? super E > c) { 2 // Convert list into array 3 Object[] a = this.toArray(); 4 // Call Arrays.sort on newly allocated array 5 Arrays.sort(a, (Comparator) c); 6 ListIterator < E > i = this.listIterator(); 7 for (Object e: a) { 8 i.next(); 9 i.set((E) e); 10 } 11}
Time Complexity
As of Java 8, Arrays.sort
uses two sorting algorithms. One is a modification of Quicksort named dual-pivot quicksort, the other an adaptation of MergeSort named Timsort. Both have a time complexity of O(n log n)
, where n
is the total number of items in the array.
With a Comparator
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. Programmer-defined comparators with little computational work also resolve to an O(1)
time complexity.
Let's say you define a comparator that had to search through files for a time complexity of O(f)
, where f
is the number of files. The time complexity for sorting results to O(n * (log n) * (O(f) + O(f))
or O(n * (log n) * O(f))
, accounting for the O(f)
algorithm that runs on each comparison.
Which Algorithm is Used
The parameter type decides the chosen sorting algorithm. Take note of Arrays.sort
s method signatures below.
Quicksort1public static void sort(int[] a) { 2 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 3} 4 5public static void sort(char[] a) { 6 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 7} 8 9public static void sort(float[] a) { 10 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 11}
Timsort1public static void sort(T[] a, Comparator<? super T> c) { 2 if (c == null) { 3 sort(a); 4 } else { 5 if (LegacyMergeSort.userRequested) 6 legacyMergeSort(a, c); 7 else 8 TimSort.sort(a, 0, a.length, c, null, 0, 0); 9 } 10}
We learn that Quicksort accepts primitive data types while Timsort accepts generics. The reason being Timsort is a stable sorting algorithm while Quicksort isn't. A stable sorting algorithm means if two items of equal value sit next to each other, they will maintain the same order after sorting.
When dealing with primitive data types, two integers swapping positions doesn't matter since they are essentially equal and you can't notice a difference. On the other hand, when sorting objects, equal objects unnecessarily swapping positions can cause harmful effects. Not to mention objects can contain distinct properties which can identify when a swap is made.
Conclusion
-
Arrays.sort
works an array andCollections.sort
works onList
s. -
Collections.sort
convertsList
s into arrays then callsArrays.sort
. -
Arrays.sort
has two different sorting algorithms. Quicksort, a non-stable algorithm, and Timsort, a stable algorithm. Both share a time complexity ofÂO(n log n)
, where n is the total number of items in the array. -
Including the comparator is
O(n * (log n) * c
, where c is the time complexity of the comparator. -
Arrays.sort
determines which sorting algorithm to use based on the type of parameter. Quicksort for primitive data types and Timsort for objects. -
A stable sorting algorithm means items of equal value stay in the same order after sorting.
About the Author
I'm Gregory Gaines, a simple software engineer @Google who's trying to write good articles. If you want more content, follow me on Twitter at @GregoryAGaines.
Now go create something great! If you have any questions, hit me up on Twitter (@GregoryAGaines); we can talk about it.
Consider signing up for my newsletter or supporting me if this was helpful.
Thanks for reading!