Insert Sort Analysis and Summation

I am trying to understand Insertion's worst sorting analysis, and I have a problem with the math involved in slide 21 (ppt) .

I understand the first formula:

Σ (j = 1 to n) j = n (n + 1) / 2

But with this I struggle:

  • Why is there - 1 at the end?
    Σ (j = 2 to n) j = n (n + 1) / 2-1
  • Also, I do not understand this:
    Σ (j = 2 to n) (j-1) = n (n-1) / 2
+4
source share
2 answers

This is a gauss trick for summing numbers from 1 to n:

trick of gauss

First formula

However, the amount you want to calculate starts with 2 , not 1 , so you must subtract the first term (i.e. 1) from the formula:

enter image description here

Second formula

Essentially, you calculate the sum from 1 to (n-1). If you replace n gauss trick by n-1 , you get the second formula that they use.

You can also see this with index conversion:

enter image description here

As you can see, I adjusted the boundaries of the sum: both the upper and lower boundaries of the sum were reduced by 1. Effectively, this reduces all terms in the sum by 1, to fix this, you have to add 1 to the term under Σ: (j-1) + 1 = j .

+6
source

Σ(j=2 to n) j=n(n+1)/2-1 starts with 2 instead of 1. Thus, the same terms are combined together except 1. Thus, the sum is 1.

Σ(j=2 to n)(j-1) are the same terms as Σ(j=1 to n-1)(j) . So, to find its sum, replace n with n-1 in the formula n(n+1)/2 .

0
source

Source: https://habr.com/ru/post/1435490/


All Articles