Thursday, July 8, 2010

ArrayList vs. LinkedList

There has been quite a bit discussion already. So just sum up by a few words:

ArrayList
  • Lookup by index is O(1). (in Java, it implements RandomAccess) Suitable for dataset with random access pattern.
  • Insertion can be expensive. Worst case O(n).
    1. Insertion to the middle can cause shifting.
    2. Insertion to the end can cause changing in the size of the internal storage, usually doubling.
  • Deletion Depending on the implementation, it may vary a bit. Generally not bad.
LinkedList
  • Lookup by index is O(n) in worst case. (in Java, it implements Queue). Suitable for data set that most of operations against it is iteration, or access mostly happens at the beginning or the end, such as FIFO queues, FILO stacks.
  • Insertion is O(1)
  • Deletion
    1. Deletion by reference is O(1)
    2. Deletion by value is as bad as lookup.
General thoughts:

Comparing two data structures, always at least think from two perspectives:
  1. operations: lookup, insertion, deletion, manipulation.
  2. input data characteristics: matching the characteristics with the cost of each operation to find the better data structure to solve your problem.

No comments: