What is the proof of of (N–1) + (N–2) + (N–3) + ... + 1= N*(N–1)/2

skystar7 picture skystar7 · Mar 20, 2010 · Viewed 96.8k times · Source

I got this formula from a data structure book in the bubble sort algorithm.

I know that we are (n-1) * (n times), but why the division by 2?

Can anyone please explain this to me or give the detailed proof for it.

Thank you

Answer

Steve314 picture Steve314 · Mar 20, 2010

Start with the triangle...

    *
   **
  ***
 ****

representing 1+2+3+4 so far. Cut the triangle in half along one dimension...

     *
    **
  * **
 ** **

Rotate the smaller part 180 degrees, and stick it on top of the bigger part...

    **
    * 

     *
    **
    **
    **

Close the gap to get a rectangle.

At first sight this only works if the base of the rectangle has an even length - but if it has an odd length, you just cut the middle column in half - it still works with a half-unit-wide twice-as-tall (still integer area) strip on one side of your rectangle.

Whatever the base of the triangle, the width of your rectangle is (base / 2) and the height is (base + 1), giving ((base + 1) * base) / 2.

However, my base is your n-1, since the bubble sort compares a pair of items at a time, and therefore iterates over only (n-1) positions for the first loop.