1 / 17

Computer Science 101 A Survey of Computer Science

Computer Science 101 A Survey of Computer Science. Efficiency of Divide and Conquer. Background - Logarithms (Base 2). Definition. The logarithm to base 2 of n is that number k such that 2 k =n. Example: 2 5 =32 so Lg(32)=5.

silver
Download Presentation

Computer Science 101 A Survey of Computer Science

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. Computer Science 101A Survey of Computer Science Efficiency of Divide and Conquer

  2. Background - Logarithms (Base 2) • Definition. The logarithm to base 2 of n is that number k such that 2k=n. • Example: 25=32 so Lg(32)=5. • Another way to think of this is that Lg(n) is the number of times we must divide n by 2 until we get 1. • Note: Lg(n) is usually a fraction, but the closest integer will work for us.

  3. Base 2 Logarithms - Table • n Lg(n) n Lg(n)1 0 1024 102 1 2048 114 2 4096 128 3 8192 1316 4 1,048,576 2032 564 6128 7256 8512 9

  4. Quicksort - Rough Analysis • For simplification, assume that we always get even splits when we partition. • When we partition the entire list, each element is compared with the pivot - approximately n comparisons.

  5. Quicksort - Rough Analysis (cont.) • Each of the “halves” is partitioned, each taking about n/2 comparisons, thus about n more comparisons. • Each of the “fourths” is partitioned,each taking about n/4 comparisons - n more.

  6. Quicksort - Rough Analysis (cont.) • How many “levels” of “about n comparisons” do we get? • Roughly, we keep splitting until the pieces are about size 1.

  7. Quicksort - Rough Analysis (cont.) • How many times must we divide n by 2 before we get 1? • Lg(n) times, of course. • Thus Comparisons  n Lg(n) • Quicksort is O(n Lg(n)) and T(n)  KnLg(n) (Linearithmic)

  8. Growth rates

  9. Binary Search Algorithm • Note: For searching sorted list • Given: n, N1,N2,…Nn (sorted) and target T • Want: Position where T is located or message indicating that T is not in list

  10. Binary Search - The algorithm Set Found to falseSet B to 1Set E to nWhile not Found and B ≤ E Set M to (B+E)/2 (whole part) If N(M)=T then Print M Set Found to true else If T<NM then Set E to M-1 else Set B to M+1end-of-loopIf not Found then Print “Target not in list”

  11. 40 42 50 63 70 85 M=(B+E)/2 4 8 20 26 31 39 40 42 50 63 70 85 Target 50 4 8 20 26 31 39 40 42 50 63 70 85 B=1 4 8 20 26 31 39 40 42 50 63 70 85 E=n 4 8 20 26 31 39 40 42 50 63 70 85 M=(B+E)/2 4 8 20 26 31 39 40 42 50 63 70 85 T>NM B=M+1 Binary Search Example Output location 9

  12. 4 8 20 26 31 39 40 42 50 63 70 85 Target 31 4 8 20 26 31 39 40 42 50 63 70 85 B=1,E=n 4 8 20 26 31 39 40 42 50 63 70 85 T<NM E=M-1 4 8 20 26 31 39 40 42 50 63 70 85 M=(B+E)/2 4 8 20 26 31 M=(B+E)/2 4 8 20 26 31 T>NM B=M+1 Binary Search Example

  13. 26 31 M=(B+E)/2 26 31 T>NM B=M+1 31 M=(B+E)/2 Binary Search Example (cont.) Output location 5

  14. Efficiency - Binary Search • Note with 1000 elements, Sequential Search would have a worst case number of comparisons of 1000. • After 1 comparison, Binary Search would be left with at most 500 elements. • After 2 comparisons 250 at worst • After 3 comparisons 125 at worst • After 4 comparisons 62 at worst • After 5 comparisons 31 at worst • After 6 comparisons 15 at worst • After 7 comparisons 7 at worst • After 8 comparisons 3 at worst • After 9 comparisons 1 at worst

  15. Efficiency - Binary Search (cont) • Each time we make a comparison, we cut the list in half. • How many times must we do this to end up with 1 element? • Lg(n), of course. • Binary search is O(Lg(n)) • T(n)  K Lg(n)

  16. Growth rates

  17. If it takes that long for 100, we'll dang well be here all night!

More Related