Insertion Sort CSE 331 Section 2 James Daly
Insertion Sort Basic idea: keep a sublist sorted, then add new items into the correct place to keep it sorted Sorted Part 2 5 Next
Insertion Sort Algorithm InsertionSort(A): for j = 2 to len(A): key ← A[j] i ← j – 1 while i > 0 and A[i] > key: A[i + 1] ← A[i] i ← i – 1 A[i + 1] ← key Total running time:
Running Times Min value of T(n) [best case] t j = 1 Max value of T(n) [worst case] t j = j
Worst-case and average-case analysis The longest running time for any input of size n = worst case Eg for insertion sort The upper-bound on the running time for any input
Worst-case and average-case analysis The worst case occurs often Eg. Database search: failed to find a match The average case is often roughly as bad as the worst case Eg. Insertion sort: roughly half elements on either side of key
Some caveats List.length() Multiplying two matrices
Simplifications / Approximations n3/2 n 2 3/2 n 2 + 7/2 n – 4% Difference % 503,7503,9214.4% 10045,00015,4362.3% ,000376,7460.5%
Big-Oh Notation (Asymptotic upper bound) f(n) = O(g(n)) iff there exists Constant c > 0 Constant n 0 Such that f(n) ≤ c g(n) for all n ≥ n 0 Eventually, always smaller than g(n)
Examples
Big-Oh Notation To show that f(n) = O(g(n)), you need to provide c and n 0 and show f(n) ≤ c g(n) for all n ≥ n 0
Example Example: n 2 + 7n + 5 = O(n 2 ) Method 1: f(n) = n 2 + 7n + 5 ≤ n n 2 + 5n 2 = 13n 2 when n ≥ 1 Thus f(n) ≤ 13n 2 when n ≥ 1
Example Example: n 2 + 7n + 5 = O(n 2 ) Method 2: f(n) ≤ c g(n) → f(n) - c g(n) ≤ 0 when n ≥ n 0 Let c = 2 and n 0 = 8 n 2 + 7n + 5 – 2n 2 = 0 when n ≈ 7.65 Be sure to check the derivative is negative -n + 7 < 0 when n ≥ 8
Big-Omega (Asymptotic lower bound) f(n) = Ω(g(n)) iff there exists Constant c > 0 Constant n 0 Such that c g(n) ≤ f(n) for all n ≥ n 0 Eventually, always bigger than g(n) Reverse of O(g(n))
Big-Theta (Asymptotic tight bound) f(n) = Θ (g(n)) iff f(n) = O(n) and f(n) = Ω(g(n))
Examples
Tricks for proving f(n) = O(g(n)) Observe the highest order term Highest term in f(n) must be ≤ that of g(n) Try fixing c first, then find n 0. Let a be the coefficient of the highest term in f(n) Try to let c = a, a+1, etc. Or let c = sum of all coefficients
Selection Sort Another sorting method Find the smallest unsorted item, then move it to the front Then find the next smallest, and so on
SelectionSort(A) for i = 1 to len(A): minj ← i for j = i + 1 to len(A): if A[j] < A[minj]: minj ← j Swap(A, i, minj)