Download presentation

Presentation is loading. Please wait.

1
CSSE221: Software Dev. Honors Day 20 Announcements Announcements Homework 7 due beginning of next class. Homework 7 due beginning of next class. 6 short written problems on data structures 6 short written problems on data structures Sierpinski gasket Sierpinski gasket Fibonacci exercise on recursion and tail recursion Fibonacci exercise on recursion and tail recursion

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

3
Search I’m thinking of a number between 1 and 100. I’m thinking of a number between 1 and 100. When you guess incorrectly, I’ll say higher or lower When you guess incorrectly, I’ll say higher or lower What’s your strategy? What’s your strategy? How does your strategy change when your only feedback is yes/no? 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. 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) How many guesses does it take in the best case? Worst-case? (Exact numbers) Get with a partner and devise scenarios for each Get with a partner and devise scenarios for each How does this change if the feedback is in the form yes/no? How does this change if the feedback is in the form yes/no?

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

6
Binary Search Divide-and-conquer Divide-and-conquer Useful only when the data is sorted (objects must be Comparable). Useful only when the data is sorted (objects must be Comparable). Guess the middle position. Guess the middle position. If it’s correct, return that 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 small, then recurse on the first half of the array If it’s too big, then recurse on the second 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 Sequential search: n/2 = O(n) each time Binary Search: O(nlog(n)) once to sort + O(log 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? 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. 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. Claims that about 70% of all CPU time is spent on these activities.

Similar presentations

© 2024 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google