Hi I'm trying to calculate the big O notation of this code, assuming the list that is being used is a linked list
// compute and apply acceleration for (int i = 0; i < list.size(); i++) { Star s1 = list.get(i); for (int j = i + 1; j < list.size(); j++) { Star s2 = list.get(j); Vector acceleration = attractionAcceleration(s1, s2); s1.velocity.add(acceleration); acceleration.negate(); s2.velocity.add(acceleration); } } // apply velocity for (int i = 0; i < list.size(); i++) { Star s = list.get(i); s.location.add(s.velocity); } } }
I was also asked to calculate the big O assuming the list is an array based list and I got O(N^2) due to it being 2 nested loops. I have been told that the answer for it as a linked list is O(N^4), i'm just not sure how I explain either of these calculations completely
https://stackoverflow.com/questions/66740218/calculating-big-o-and-time-complexity-of-code-as-a-linked-list-vs-array-based March 22, 2021 at 12:07PM
没有评论:
发表评论