Remarks

When we analyze the performance of the sorting algorithm, we interest primarily on the number of comparison and exchange.

Average Exchange

Let En be the total average number of exchange to sort array of N element. E1 = 0 (we do not need any exchange for array with one element). The average number of exchange to sort N element array is the sum of average number of number of exchange to sort N-1 element array with the average exchange to insert the last element into N-1 element array.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/95690508-92fd-4aef-851a-fd8b4621362e/Untitled.png

Simplify the summation (arithmetic series)

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/767dc83e-5e47-44b1-a9df-b6a1498ea8f1/Untitled.png

Expands the term

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/dadc8deb-15d7-453d-95c9-203967d49198/Untitled.png

Simplify the summation again (arithmetic series)

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/fe80e0fd-66fd-4f35-8f73-5e0f62faeae7/Untitled.png

Average Comparison

Let Cn be the total average number of comparison to sort array of N element. C1 = 0 (we do not need any comparison on one element array). The average number of comparison to sort N element array is the sum of average number of number of comparison to sort N-1 element array with the average comparison to insert the last element into N-1 element array. If last element is largest element, we need only one comparison, if the last element is the second smallest element, we need N-1 comparison. However, if last element is the smallest element, we do not need N comparison. We still only need N-1 comparison. That is why we remove 1/N in below equation.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/d4cf8b3b-8590-466a-a685-839c58328c45/Untitled.png

Simplify the summation (arithmetic series)

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/f3d4dcc3-5cb9-417f-8eea-08736ff10098/Untitled.png

Expand the term

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/0bbf6656-0274-46f6-b024-fb80457a4c4f/Untitled.png

Simplify the summation again (arithmetic series and harmonic number)

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/6cc56ed4-878d-42a3-bd92-465d55ec69c4/Untitled.png