Insertion Sort

by Jerry Sky

2020-03-02



Insertion sort(A,n)

A={a1,a2,...an}A = \{ a_1, a_2, ... a_n \}

for j = 1 to n {
  key = A[j]
  i = j-1
  while( i >= 0 && A[i] > key) {
    A[i+1] = A[i]
    i--
  }
  A[i+1] = key
}

kroki:

[X][ ][ ]...[ ]
 1  2  3 ... n
       m
[X][X][ ]...[a_k]|...[ ]
 1  2  3 ...k   ... n

Worst Case Analysis

max(max( Liczba porównań pomiędzy elementami input’u A)A)

Example

[9, 7, 5, 3, 1]
 1  2  3  4  5
[5, 7, 9, 3, 1]

T(n)=j=1n1O(j)=O(j=1n1j)=() T(n) = \sum^{n-1}_{j=1} O(j) = O\Bigg( \sum^{n-1}_{j=1}j \Bigg) = (*) przy czym j=1n1j=(n2)=n(n1)2 \sum^{n-1}_{j=1}j = \binom{n}{2} = \frac{n(n-1)}{2} dlatego ()=O(n2) (*) = O(n^2)

Average Case Analysis