This commit is contained in:
Luke Butters 2017-11-01 16:19:03 +11:00
parent 773ff39e48
commit e4d87b4b07

View File

@ -9,16 +9,15 @@ import java.util.List;
* <p>Removing elements from an ArrayList in a naive way can lead to O(n^3) * <p>Removing elements from an ArrayList in a naive way can lead to O(n^3)
* running time. If the algorithm first creates a list of all the elements * running time. If the algorithm first creates a list of all the elements
* to remove, then we for each element in this list (assume n elements) we look * to remove, then we for each element in this list (assume n elements) we look
* for in element in the list (against n elements) and when found we need to remove * for the element in the original list (against n elements) and when found we need
* the element and move the elements to the right one to the left the size of this * to remove the element and move the elements to the right (of the removed element)
* operation is at worst n hence O(n^3).</p> * to the left by one, the size of this operation is at worst n hence O(n^3).</p>
* *
* <p>This basically avoids that by making a new list and copying over only elements * <p>This instead makes a new list and copies over only elements we want to keep,
* we want to keep, we then clear the given list and all of the elements this gives us * we then clear the original list and then add all of the elements to the original
* (for ArrayList) O(n) running time.</p> * list. This gives us (for ArrayList) a running time of O(n).</p>
* *
* <p>The performance of this has not been thoroughly tested for linked list but * <p>The performance of this has not been thoroughly tested for linked list.</p>
* it probably is not too bad.</p>
* *
* <p>This can be completely removed in java 8 as the List#removeIf() method can be used instead * <p>This can be completely removed in java 8 as the List#removeIf() method can be used instead
* as this already is optimised for each list implementation. * as this already is optimised for each list implementation.