Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Big O Notation, what is the correct label for this algorithm?

I am curious. What is the correct way to describe this using Big-O Notation?

var prices = [100, 180, 260, 590, 40, 310, 535, 10, 5, 3];
var biggest_profit = 0;
  
for (var i = 0; i < prices.length; i++) {
    var first_price = prices[i];
  
    for (var j = i + 1; j <= prices.length; j++) {
      // do something here
    }
}
  

This is the bit that throws me off:

j = i + 1

Every time we go through i, the j becomes shorter and shorter.

What is the correct name for this pattern in Big O Notation?

like image 750
lars Avatar asked Aug 26 '26 11:08

lars


1 Answers

You can use Sigma notation to calculate the number of visits of the inner loop ("do something here")

enter image description here

Where (*) follows from a summation rule made famous by the rumour that Gauss once derived it on-the-spot as a young student.

like image 96
dfrib Avatar answered Aug 30 '26 01:08

dfrib



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!