Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Insertion Sort CSE 331 Section 2 James Daly

2 Insertion Sort Basic idea: keep a sublist sorted, then add new items into the correct place to keep it sorted 13461346 Sorted Part 2 5 Next

3 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:

4 Running Times Min value of T(n) [best case] t j = 1 Max value of T(n) [worst case] t j = j

5 Worst-case and average-case analysis The longest running time for any input of size n = worst case Eg. 5 3 2 1 0 for insertion sort The upper-bound on the running time for any input

6 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

7 Some caveats List.length() Multiplying two matrices

8 Simplifications / Approximations n3/2 n 2 3/2 n 2 + 7/2 n – 4% Difference 1015018117% 503,7503,9214.4% 10045,00015,4362.3% 500375,000376,7460.5%

9 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)

10 Examples

11

12 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

13 Example Example: n 2 + 7n + 5 = O(n 2 ) Method 1: f(n) = n 2 + 7n + 5 ≤ n 2 + 7 n 2 + 5n 2 = 13n 2 when n ≥ 1 Thus f(n) ≤ 13n 2 when n ≥ 1

14 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

15 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))

16 Big-Theta (Asymptotic tight bound) f(n) = Θ (g(n)) iff f(n) = O(n) and f(n) = Ω(g(n))

17 Examples

18

19 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

20 Selection Sort Another sorting method Find the smallest unsorted item, then move it to the front Then find the next smallest, and so on

21 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)


Download ppt "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."

Similar presentations


Ads by Google