I have a midterm next week, and some of it has to do with code analysis/ sum simplification. I'm very lost, and am trying to understand this question my professor gave us on a practice work sheet.
Here is the pseudo code:
List <Integer> method( List <Integer> ints) {
for ( int i = 0; i < ints.size() / 2; i ++) {
swap(i, n − i − 1);
}
The question is asking: Express the worst case running time of this method as a sum assuming that the List is an ArrayList? In which I got O(log n), since the size of the list is being divided in half every time.
But then the next question is: Express the worst case running time of this method as a sum assuming that the List is an LinkedList? Now I am confused, I know that ArrayLists and LinkedLists have different time complexities, but wouldn't the answer be the same O(log n)?
Also how would I express this as a sum for each question? This is not homework, but I am trying my best to understand this subject.
intsis an ArrayList, it would just be O(n). If it is a LinkedList, it would be O(n^2), because you have to traverse the list again for each element. There should not be any logs because the length only gets cut in half once - not every iteration.