1 / 24

Source: https://en.wikipedia/wiki/Insertion_sort#/media/File:Insertion_sort.gif

Source: https://en.wikipedia.org/wiki/Insertion_sort#/media/File:Insertion_sort.gif. Source: http://math.hws.edu/eck/cs124/javanotes3/c8/fig4.gif. Insertion Sort. for (int i = 1; i < n; i++) {// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--)

ktrudel
Download Presentation

Source: https://en.wikipedia/wiki/Insertion_sort#/media/File:Insertion_sort.gif

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Source: https://en.wikipedia.org/wiki/Insertion_sort#/media/File:Insertion_sort.gif

  2. Source: http://math.hws.edu/eck/cs124/javanotes3/c8/fig4.gif

  3. Insertion Sort for (int i = 1; i < n; i++) {// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t; }

  4. Complexity • Space/Memory • Time • Count a particular operation • Count number of steps • Asymptotic complexity • What could be more important than performance? • Maintainability, robustness, extensibility, programmer time, etc.

  5. Comparison Count for (int i = 1; i < n; i++) {// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t; }

  6. Comparison Count • Pick an instance characteristic … n • For insertion sort, pick n = number of elements to be sorted. • Determine count as a function of this instance characteristic.

  7. Comparison Count for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; How many comparisons are made?

  8. Comparison Count for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; number of compares depends on a[]s and t as well as on i

  9. Comparison Count • Worst-case count = maximum count • Best-case count = minimum count • Average count

  10. Worst-Case Comparison Count for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a = [1, 2, 3, 4] and t = 0 => 4 compares a = [1,2,3,…,i] and t = 0 => i compares

  11. Worst-Case Comparison Count for (int i = 1; i < n; i++) for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; total compares = 1 + 2 + 3 + … + (n-1) = (n-1)n/2

  12. Step Count A step is an amount of computing that does not depend on the instance characteristic n 10 adds, 100 subtracts, 1000 multiplies can all be counted as a single step 10n  O(n), 100n  O(n), etc. n adds cannot be counted as 1 step

  13. Step Count Steps per execution for (int i = 1; i < n; i++)1 {// insert a[i] into a[0:i-1] 0 int t = a[i]; 1 int j; 0 for (j = i - 1; j >= 0 && t < a[j]; j--) 1 a[j + 1] = a[j]; 1 a[j + 1] = t; 1 } 0 for (int i = 1; i < n; i++) {// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t; }

  14. Step Count s/e isn’t always 0 or 1 x = sum(a[], n); where n is the instance characteristic has a s/e count of n (sum() is Program 1.30).

  15. Step Count s/e steps for (int i = 1; i < n; i++)1 {// insert a[i] into a[0:i-1] 0 int t = a[i]; 1 int j; 0 for (j = i - 1; j >= 0 && t < a[j]; j--) 1 a[j + 1] = a[j]; 1 a[j + 1] = t; 1 } 0 for (int i = 1; i < n; i++) {// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t; } i+ 1 i

  16. Step Count step count for for (int i = 1; i < n; i++) is n i = 1 i = 2 … 1+2+3+…+n = n(n-1)/2

  17. Space Complexity • O(n) total • O(1) additional space Source: https://en.wikipedia.org/wiki/Insertion_sort#/media/File:Insertion-sort-example-300px.gif

  18. Asymptotic Complexity of Insertion Sort • O(n2) • What does this mean?

  19. Complexity of Insertion Sort • Time or number of operations does not exceed c.n2 on any input of size n (n suitably large)(c constant). • Actually, the worst-case time is Q(n2) and the best-case is Q(n) • So, the worst-case time is expected to quadruple each time n is doubled

  20. Complexity of Insertion Sort • Is O(n2) too much time? • Is the algorithm practical?

  21. Practical Complexities 109 instructions/second

  22. Impractical Complexities 109 instructions/second Age of the universe: 13.8 * 10^9 years!*

  23. Faster Computer Vs Better Algorithm Algorithmic improvement more useful than hardware improvement. E.g. 2n to n3

  24. To-do • Read: • https://en.wikipedia.org/wiki/Big_O_notation • Do: • Create GitHub (or similar) account • Insert 0 into 1 2 3 4 5 – Count Steps • Insert 10 into 1 2 3 4 5 – Count Steps • Implement (ungraded): • Insertion Sort (C++) • Sort a sorted sequence/reversely sorted sequence (n=1000,1000000), what’s the difference in runtime?

More Related