Collections vs Arrays regarding sort()

Collections.sort() Operates on List Whereas Arrays.sort() Operates on an Array.

Arrays.sort() uses Dual-Pivot Quicksort for Primitive Arrays and MergeSort for sorting array of Objects.

Example of Collections.sort() :

 ArrayList<Integer> arr = new ArrayList<Integer>();
 arr.add(15);
 arr.add(10);
 arr.add(5); 
 arr.add(2); 

 Collections.sort(arr);

Example of Arrays.sort() :

int[] arr = new int[4]
 arr[0]=15;
 arr[1]=10;
 arr[2]=5; 
 arr[3]=2; 

 Arrays.sort(arr);

I know Arrays' sort() is using binary search for sort()

No, you don't know any such thing. It doesn't do that. See the Javadoc.

The statement doesn't even make sense. You can't 'use binary search for sort'. Binary search only worked when the data is already sorted. Maybe what you read is that Arrays.binarySearch() assumes the data is sorted.


Well, besides operating on different stuff (Collections.sort operates on a List, and Arrays.sort operates on an array), java.util.Collections.sort() simply calls java.util.Arrays.sort() to do the heavy lifting.

Also, for what it's worth, notice that Arrays.sort runs a merge sort.

Tags:

Java