clever uses of binary search
play

Clever Uses of Binary Search Section 3.3 Dr. Mayfield and Dr. Lam - PowerPoint PPT Presentation

Clever Uses of Binary Search Section 3.3 Dr. Mayfield and Dr. Lam Department of Computer Science James Madison University Oct 23, 2015 Guessing games Im thinking of a number between 1 and 100 Im thinking of a last name of a JMU


  1. Clever Uses of Binary Search Section 3.3 Dr. Mayfield and Dr. Lam Department of Computer Science James Madison University Oct 23, 2015

  2. Guessing games ◮ I’m thinking of a number between 1 and 100 ◮ I’m thinking of a last name of a JMU student Oct 23, 2015 Clever Uses of Binary Search 2 of 9

  3. STL implementation #include <algorithm> typedef T int; // or string, whatever T value; bool found; vector<T> v; ... sort(v.begin(), v.end()); // don ' t forget to sort! found = binary_search(v.begin(), v.end(), value); http://www.cplusplus.com/reference/algorithm/binary search/ Oct 23, 2015 Clever Uses of Binary Search 3 of 9

  4. Java implementation import java.util.*; T value; int index; List<T> v; ... Collections.sort(v); // don ' t forget to sort! index = Collections.binarySearch(v, value); // returns a negative value if not found http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html Oct 23, 2015 Clever Uses of Binary Search 4 of 9

  5. Iterative solutions Basic idea: hi = initial_high_guess(); lo = initial_low_guess(); while (hi > lo) { mid = (hi - lo) / 2 + lo; if (is_less_than(mid)) { hi = mid; // answer is in [lo,mid) } else { lo = mid; // answer is in [mid,hi] } } Trick: mid = (hi - lo) / 2 + lo; // intuitive mid = (hi + lo) / 2; // same but quicker // and less error-prone Oct 23, 2015 Clever Uses of Binary Search 5 of 9

  6. Iterative solution #1 // adjustable threshold #define EPSILON 1e-9 while (hi - lo > EPSILON) { double mid = (lo + hi) / 2.0; if (/* depends on the problem */) hi = mid; // guess lower else lo = mid; // guess higher } return hi; Be careful with floating point precision ◮ Correct up to n decimal places ◮ Remember that printf rounds Oct 23, 2015 Clever Uses of Binary Search 6 of 9

  7. Iterative solution #2 for (i = 0; i < 50; i++) { double mid = (lo + hi) / 2.0; if (/* depends on the problem */) hi = mid; // guess lower else lo = mid; // guess higher } return hi; The book points out that log 2 ((10000 − 0) / 10 − 9 ) ≈ 43 ◮ Constant number of iterations avoids precision errors ◮ Avoids the case when #1 results in an infinite loop ◮ Plus you don’t need the check overhead every time Oct 23, 2015 Clever Uses of Binary Search 7 of 9

  8. Bisection method http://en.wikipedia.org/wiki/Bisection method Oct 23, 2015 Clever Uses of Binary Search 8 of 9

  9. Binary search the answer Oct 23, 2015 Clever Uses of Binary Search 9 of 9

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend