Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collections vs Arrays regarding sort()

Tags:

java

Collections vs Arrays regarding sort() What is the difference between these two regarding sort() method? I know Arrays' sort() is using binary search for sort(), what about Collections'? And how to choose which to use? Thanks!

like image 434
Josh Morrison Avatar asked Mar 06 '11 01:03

Josh Morrison


People also ask

Does Collections sort work on arrays?

sort() vs Collections. sort works for arrays which can be of primitive data type also.

Which is best for sorting in Collections?

If you just want to sort a list, use any kind of List and use Collections. sort(). If you want to make sure elements in the list are unique and always sorted, use a SortedSet.

What is the difference between Collections and arrays?

Arrays can hold the only the same type of data in its collection i.e only homogeneous data types elements are allowed in case of arrays. Collection, on the other hand, can hold both homogeneous and heterogeneous elements. Arrays can hold both object and primitive type data.

What does collection sort () do?

Collections sort is a method of Java Collections class used to sort a list, which implements the List interface. All the elements in the list must be mutually comparable. If a list consists of string elements, then it will be sorted in alphabetical order.


6 Answers

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.

like image 95
Mark Elliot Avatar answered Oct 05 '22 18:10

Mark Elliot


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);
like image 25
Sunny Avatar answered Oct 05 '22 20:10

Sunny


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.

like image 34
user207421 Avatar answered Oct 05 '22 18:10

user207421


Use Arrays.sort() if you're dealing with an Array. Use Collections.sort() if you're dealing with something that implements the Collection interface (eg ArrayList).

like image 37
Seth Hoenig Avatar answered Oct 05 '22 19:10

Seth Hoenig


As the other answers have said, you would use Collections.sort() when dealing with an object that implements the Collection interface and the Arrays.sort() method when dealing with an Array.

A related question is what type of data structures are better if you want to sort a set of values. If you need to use a List, then I would suggest using a LinkedList since insertions run in O(1) where something like an ArrayList would be O(n).

You could also opt for using a SortedSet if there will be no duplicates or having duplicates is unwanted. That way you don't have to bother with using an external sort method.

like image 35
salexander Avatar answered Oct 05 '22 19:10

salexander


Collection.sort is used when you dealing with lists and arrays.sort is sued when dealing with arrays. But internally Collection.sort uses Arrays.sort method only. Now internally sorting is done based in Timosrt technique instead of merge sort , because stable, adaptive arrays takes O(nlogn) comparisons in Merge sort but in Timsort worst case it will take O(nlogn) and in worst case storage requies n/2 in Timsort but it is not in the case of Merge sort technique.

like image 40
Ranga Reddy Avatar answered Oct 05 '22 19:10

Ranga Reddy