1 / 30

Text Chapters 1, 2

Text Chapters 1, 2. Sorting. Algorithm : well-defined computational procedure that transforms input into output steps for the computer to follow to solve a problem. Sorting Problem: Input: A sequence of n numbers

senona
Télécharger la présentation

Text Chapters 1, 2

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. Text Chapters 1, 2

  2. Sorting • Algorithm: • well-defined computational procedure that transforms input into output • steps for the computer to follow to solve a problem • Sorting Problem: • Input: A sequence of n numbers • Output: A permutation (reordering) of the input sequence such that: instance

  3. Insertion Sort Animation Finding a place for item with value 5 in position 1: Swap item in position 0 with item in position 1. http://www.cs.brockport.edu/cs/java/apps/sorters/insertsortaniminp.html

  4. Insertion Sort Animation Positions 0 through 1 are now in non-decreasing order. http://www.cs.brockport.edu/cs/java/apps/sorters/insertsortaniminp.html

  5. Insertion Sort Animation Finding a place for item with value 1 in position 2: Swap item in position 1 with item in position 2. http://www.cs.brockport.edu/cs/java/apps/sorters/insertsortaniminp.html

  6. Insertion Sort Animation Finding a place for item with value 1: Swap item in position 0 with item in position 1. Positions 0 through 2 are now in non-decreasing order. http://www.cs.brockport.edu/cs/java/apps/sorters/insertsortaniminp.html

  7. Insertion Sort Animation Finding a place for item with value 3 in position 3: Swap item in position 2 with item in position 3. http://www.cs.brockport.edu/cs/java/apps/sorters/insertsortaniminp.html

  8. Insertion Sort Animation Finding a place for item with value 3: Swap item in position 1 with item in position 2. http://www.cs.brockport.edu/cs/java/apps/sorters/insertsortaniminp.html

  9. Insertion Sort Animation Positions 0 through 3 are now in non-decreasing order. http://www.cs.brockport.edu/cs/java/apps/sorters/insertsortaniminp.html

  10. Insertion Sort Animation Finding a place for item with value 2 in position 4: Swap item in position 3 with item in position 4. http://www.cs.brockport.edu/cs/java/apps/sorters/insertsortaniminp.html

  11. Insertion Sort Animation Finding a place for item with value 2: Swap item in position 2 with item in position 3. http://www.cs.brockport.edu/cs/java/apps/sorters/insertsortaniminp.html

  12. Insertion Sort Animation Finding a place for item with value 2: Swap item in position 1 with item in position 2. http://www.cs.brockport.edu/cs/java/apps/sorters/insertsortaniminp.html

  13. Insertion Sort Animation Positions 0 through 4 are now in non-decreasing order. http://www.cs.brockport.edu/cs/java/apps/sorters/insertsortaniminp.html

  14. Insertion Sort Animation Finding a place for item with value 6 in position 5: Swap item in position 4 with item in position 5. http://www.cs.brockport.edu/cs/java/apps/sorters/insertsortaniminp.html

  15. Insertion Sort Animation Positions 0 through 5 are now in non-decreasing order. http://www.cs.brockport.edu/cs/java/apps/sorters/insertsortaniminp.html

  16. Insertion Sort Animation Finding a place for item with value 4 in position 6: Swap item in position 5 with item in position 6. http://www.cs.brockport.edu/cs/java/apps/sorters/insertsortaniminp.html

  17. Insertion Sort Animation Finding a place for item with value 4: Swap item in position 4 with item in position 5. http://www.cs.brockport.edu/cs/java/apps/sorters/insertsortaniminp.html

  18. Insertion Sort Animation Positions 0 through 6 are now in non-decreasing order. http://www.cs.brockport.edu/cs/java/apps/sorters/insertsortaniminp.html

  19. Insertion Sort Animation Finding a place for item with value 7 in position 7: Swap item in position 6 with item in position 7. http://www.cs.brockport.edu/cs/java/apps/sorters/insertsortaniminp.html

  20. Insertion Sort Animation Positions 0 through 7 are now in non-decreasing order. http://www.cs.brockport.edu/cs/java/apps/sorters/insertsortaniminp.html

  21. Insertion Sort Animation Positions 0 through 7 are now in non-decreasing order. http://www.cs.brockport.edu/cs/java/apps/sorters/insertsortaniminp.html

  22. Insertion Sort Animation Positions 0 through 7 are now in non-decreasing order. http://www.cs.brockport.edu/cs/java/apps/sorters/insertsortaniminp.html

  23. courtesy of Prof. Costello Asymptotic Notation O(g(n)) is a set of functions, so we often say f(n) is in O(g(n)).

  24. courtesy of Prof. Costello Asymptotic Notation (cont.)

  25. Asymptotic Analysis Math fact sheet (courtesy of Prof. Costello) is on our web site.

  26. n lg(n) 2n 1 lglg(n) lg(n) n n lg2(n) n2 n5 Function Order of Growth O( ) upper bound W( ) lower bound Q( ) upper & lower bound know how to order functions asymptotically (behavior as n becomes large) know how to use asymptotic complexity notation to describe time or space complexity

  27. Types of Algorithmic Input Best-Case Input: of all possible algorithm inputs of size n, it generates the “best” result for Time Complexity: “best” is smallest running time Best-Case Input Produces Best-Case Running Time provides a lower bound on the algorithm’s asymptotic running time (subject to any implementation assumptions) for Space Complexity: “best” is smallest storage Average-Case Input Worst-Case Input these are defined similarly Best-Case Time <= Average-Case Time <= Worst-Case Time

  28. n lg(n) 2n 1 lglg(n) lg(n) n n lg2(n) n2 n5 Bounding Algorithmic Time(using cases) Using “case” we can discuss lower and/or upper bounds on: best-case running time or average-case running time or worst-case running time T(n) = W(1) T(n) = O(2n) very loose bounds are not very useful! Worst-Case time of T(n) = O(2n) tells us that worst-case inputs cause the algorithm to take at most exponential time (i.e. exponential time is sufficient). But, can the algorithm every really take exponential time? (i.e. is exponential time necessary?) If, for arbitrary n, we find a worst-case input that forces the algorithm to use exponential time, then this tightens the lower bound on the worst-case running time. If we can force the lower and upper bounds on the worst-case time to match, then we can say that, for the worst-case running time, T(n) = Q(2n ) (i.e. we’ve found the minimum upper bound, so the upper bound is tight.)

  29. n lg(n) 2n 1 lglg(n) lg(n) n n lg2(n) n2 n5 Bounding Algorithmic Time(tightening bounds) for example... TB (n) = O(n) TW (n) = W(n2) TW (n) = O(2n) TB(n) = W(1) 1st attempt 1st attempt 1st attempt 1st attempt TB(n) = Q(n) TW(n) = Q(n2) 2nd attempt 2nd attempt Algorithm Bounds Here we denote best-case time by TB(n); worst-case time by TW(n)

  30. 2n W(n) 1 O(n5) No algorithm for the problem exists that can solve it for worst-case inputs in less than linear time . An inefficient algorithm for the problem might exist that takes this much time, but would not help us. worst-case bounds on problem Know the Difference! Strong Bound: A worst-case lower bound on a problem holds for every algorithm that solves the problem and abides by the problem’s assumptions. Weak Bound: A worst-case upper bound on a problem comes from just considering one algorithm. Other, less efficient algorithms that solve this problem might exist, but we don’t care about them! Both the upper and lower bounds could be loose (i.e. perhaps could be tightened later on).

More Related