I'm sure this must be a typical example but I'm very new to calculating O notations from an algorithm. Ideally, the answer to this question wouldn't just give the big-O notation but explain how to find it from looking at the algorithm.
// Apply Newton's law of universal gravitation
for (let n = 0; n < sky.length; n++) {
let celestial = sky[n]
for (let m = n + 1; m < sky.length; m++) {
let melancholia = sky[m]
let gravity = // Code to calculate gravity
// Apply gravity to celestial
celestial.applyGravity(gravity)
// Apply reversed gravity to melancholia
gravity.mult(-1)
melancholia.applyGravity(gravity
}
celestial.update()
}
I optimized the code from O(n^2) with let m = n + 1
instead of let m = 0
. Now, I recorded O values for certain n values and got a graph looking like: