0

I had implement bubble and quick sort algorithms and now I have to measure their performances using the following code for a LinkedList and ArrayList.

Instant start = Instant.now();
Sort…
Instant stop = Instant.now();
Duration duration = Duration.between(start, stop);
System.out.println(duration);

Why do we get different times when using ArrayList vs LinkedList ?

Denisa C.
  • 3
  • 1
  • Which one is better? `ArrayList` and `LinkedList` are different implementations of the `List` interface. They differ in structure, which causes the time-complexities of sorting (and other operations) to be different. Find more information in [this question](https://stackoverflow.com/questions/8069370/is-an-arraylist-or-a-linkedlist-better-for-sorting) – deHaar Dec 03 '18 at 13:25
  • Because you have different time complexity to access element in different types of list – Ulad Dec 03 '18 at 13:25

2 Answers2

0

Because their structure are different. For example ArrayList is faster in iteration but LinkedList is faster in insertion and deletion.

0

ArrayList and LinkedList have different implementation.

Retrieving an element with an ArrayList can be done in O(1), because you need only to know the index of the element and you can retrieve it directly from the array where it is stored. Using a LinkedList you need to iterate over the elements in the list before getting the element requested, so it can be done in O(n).

Such differences can be propagated to your sort times.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56