26

I want to use data structure that needs to be sorted every now and again. The size of the data structure will hardly exceed 1000 items.

Which one is better - ArrayList or LinkedList?

Which sorting algorithm is better to use?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Sergey Gazaryan
  • 1,013
  • 1
  • 9
  • 25
  • 5
    What is your hypothesis? What did you learn when you tested your hypothesis? – Mark Peters Nov 09 '11 at 17:59
  • at the size you are considering, either will be the same, I 100% this will **not** be a bottle neck in your application. Always profile yourself and see what you find with your data, only you can do that! –  Nov 09 '11 at 18:08
  • Wouldn't a heap be best? – Garrett Hall Nov 09 '11 at 18:13
  • Note: Poster notes in a comment below that maximum list size may grow in the future beyond 1000 items. – Andy Thomas Nov 09 '11 at 19:07
  • Related - [When to use LinkedList<> over ArrayList<>?](http://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist) – Bernhard Barker Apr 22 '14 at 08:15
  • I like how none of the answers actually answer the question. – Bernhard Barker Apr 22 '14 at 10:29
  • Take a look at this and i am sure you will come up with a conclusion for yourself. Regards! http://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist – Mechkov Nov 09 '11 at 17:59

5 Answers5

33

Up to Java 7, it made no difference because Collections.sort would dump the content of the list into an array.

With Java 8, using an ArrayList should be slightly faster because Collections.sort will call List.sort and ArrayList has a specialised version that sorts the backing array directly, saving a copy.

So bottom line is ArrayList is better as it gives a similar or better performance depending on the version of Java.

assylias
  • 321,522
  • 82
  • 660
  • 783
9

If you're going to be using java.util.Collections.sort(List) then it really doesn't matter.

If the List does not implement RandomAccess, then it will be dumped to a List The list will get dumped into an array for purposes of sorting anyway.

(Thanks for keeping me honest Ralph. Looks like I confused the implementations of sort and shuffle. They're close enough to the same thing right?)

corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • 3
    This is not what the java doc say or the implementation does (or at least how I understand them): JavaDoc "This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element" -- It dumps into an Array (not a List) and this is done, no matter if it implements RandomAccess or not. – Ralph Nov 09 '11 at 18:12
4

If you can use the Apache library, then have a look at TreeList. It addresses your problem correctly.

Adowrath
  • 701
  • 11
  • 24
Saurabh
  • 7,894
  • 2
  • 23
  • 31
3

Only 1000 items? Why do you care?

I usually always use ArrayList unless I have specific need to do otherwise.

Have a look at the source code. I think sorting is based on arrays anyway, if I remember correctly.

Puce
  • 37,247
  • 13
  • 80
  • 152
1

If you are just sorting and not dynamically updating your sorted list, then either is fine and an array will be more memory efficient. Linked lists are really better if you want to maintain a sorted list. Inserting an object is fast into the middle of a linked list, but slow into an array.

Arrays are better if you want to find an object in the middle. With an array, you can do a binary sort and find if a member is in the list in O(logN) time. With a linked list, you need to walk the entire list which is very slow.

I guess which is better for your application depends on what you want to do with the list after it is sorted.

Ron
  • 1,305
  • 2
  • 14
  • 22
  • 1
    *Inserting an object is fast into the middle of a linked list, but slow into an array.* However, finding the location to insert into takes `O(n)` time in a linked list vs `O(log n)` in an ArrayList. The complexity is a wash. In actual fact the only reason sorting a linked list in Java (i.e. `Collections.sort()`) is acceptably fast is because it dumps the entire list into an array list and sorts that, then dumps it back to a linked list. – Mark Peters Nov 09 '11 at 18:00
  • The complexity is a wash and the local access in memory is significantly (experiments I saw and tested were around 40%) faster for large data sets. – corsiKa Nov 09 '11 at 18:01