Given an array a [1,2,3,4,5,6,7,8,9,10], let's say we have an algorithm that does the following:
for i in 0..a.length for j in 0..a.length ...
This will have OO (n ^ 2) for O (n ^ 2), because for each element it will move throughout the array.
But what if the inner loop only intersects with the outer index ahead?
for i in 0..a.length for j in i..a.length ...
That is, in comparison, the first algo will look at n elements for each iteration (outer loop). The second algo looks at:
- n elements in the first iteration
- n-1 elements in the second iteration
- n-2 elements in the third iteration
- ...
- 1 element in the last iteration
When calculating the Big O runtime for this kind of algorithm, is it still O (n ^ 2)?
source share