1 / 9

CSSE221: Software Dev. Honors Day 20

CSSE221: Software Dev. Honors Day 20. Announcements Homework 7 due beginning of next class. 6 short written problems on data structures Sierpinski gasket Fibonacci exercise on recursion and tail recursion. This week: Simulation Project. Monday: Planning for Simulation Project Tuesday:

holly-moore
Download Presentation

CSSE221: Software Dev. Honors Day 20

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. CSSE221: Software Dev. Honors Day 20 • Announcements • Homework 7 due beginning of next class. • 6 short written problems on data structures • Sierpinski gasket • Fibonacci exercise on recursion and tail recursion

  2. This week: Simulation Project • Monday: • Planning for Simulation Project • Tuesday: • Intro to searching algorithms and their efficiency (last round 2 capsule) • Threads (capsule) • Thursday: • Animation (capsule) • Advanced GUIs (capsule)

  3. Search • I’m thinking of a number between 1 and 100. • When you guess incorrectly, I’ll say higher or lower • What’s your strategy? • How does your strategy change when your only feedback is yes/no?

  4. Search Efficiency • I’m thinking of a number between 1 and 100 and say “higher” or “lower” if you are incorrect. • How many guesses does it take in the best case? Worst-case? (Exact numbers) • Get with a partner and devise scenarios for each • How does this change if the feedback is in the form yes/no?

  5. Sequential search • int indexOf(int n, int[] ar) { • for (int i = 0; i < ar.length; i++) { • if (n == ar[i]) return i; • } • return -1; • } • Is the best you can do? • No, but only on one condition… • The data must be sorted.

  6. Binary Search • Divide-and-conquer • Useful only when the data is sorted (objects must be Comparable). • Guess the middle position. • If it’s correct, return that position • If it’s too small, then recurse on the first half of the array • If it’s too big, then recurse on the second half of the array.

  7. Demo

  8. Analysis of Search Time • Sequential search: n/2 = O(n) each time • Binary Search: O(nlog(n)) once to sort + O(log n) each time • Which is faster if we had an array with 1M elements and we wanted to search it 1M times?

  9. Searching and sorting are ubiquitous • In the classic book series The Art of Computer Programming, Donald Knuth devotes a whole volume (about 700 pages) to sorting and searching. • Claims that about 70% of all CPU time is spent on these activities.

More Related