1 / 8
Settings
<<<>>>
Finally, consider the average case cost.
- static void inssort(int[] A) {
- for (int i=1; i<A.length; i++) // Insert i'th record
- for (int j=i; (j>0) && (A[j] < A[j-1]); j--)
- swap(A, j, j-1);
- }
- 0
- 1
- ...
- i-1
- i
- ...
- n-1
$\displaystyle\sum_{i=1}^{n-1}\frac{i}{2}$
$n - 1$
$\displaystyle\sum_{i=1}^{n-1}\frac{i}{2} = \frac{n(n+1)}{4}$