Download presentation

Presentation is loading. Please wait.

Published byPreston Birk Modified about 1 year ago

1
Georgia Institute of Technology Speed part 5 Barb Ericson Georgia Institute of Technology May 2006

2
Georgia Institute of Technology Learning Goals Computing Concepts –Introduce searching Linear search Binary search –Show the difference between best case, worst case, and average case execution time

3
Georgia Institute of Technology Searching for Data If we were looking up a word in a dictionary –We could check each entry till we found it This is a linear search and is O(n) In the best case it would be the first word in the dictionary – 1 step In the worst case it would be the last word in the dictionary – n steps The average case is n/2 steps

4
Georgia Institute of Technology Linear Search on Strings public static String linearFind(String target, String[] list) { for (int index=0; index < list.length; index++) { if (target.compareTo(list[index]) == 0) { return("Found it at " + index); } return("Not found"); }

5
Georgia Institute of Technology Main for Testing public static void main(String[] args) { String[] searchMe = {"apple","bear","cat","dog","elephant"}; System.out.println(linearFind("apple",searchMe)); System.out.println(linearFind("cat",searchMe)); System.out.println(linearFind("giraffe",searchMe)); }

6
Georgia Institute of Technology Smart Guessing Have played a game where you have to guess a number from 1 to 100? –And the answer is either low, high or right –What is a good number to start guessing with? –What is the minimum number of guesses you can make? –What is the maximum number of guesses you need to find any number?

7
Georgia Institute of Technology Dividing the Search Space in Half If you start with 50 you can eliminate about half the numbers –If the response was high try 25 –If the response was low try 75 In each guess make your next guess about half of the current range –This is called a binary search –It is O(log n) log n = x where 2 x = n

8
Georgia Institute of Technology Binary Search on Strings public static String binaryFind(String target, String[] list) { int start = 0; int end = list.length - 1; int checkpoint = 0; while (start <= end) { //While there are more to search // find the middle checkpoint = (start+end)/2; System.out.println("Checking at: "+ checkpoint+" start="+start+" end="+end); if (target.compareTo(list[checkpoint]) == 0) { return "Found it!"; } else if (target.compareTo(list[checkpoint]) > 0) { start=checkpoint + 1; } else if (target.compareTo(list[checkpoint]) < 0) { end=checkpoint - 1; } return "Not found"; }

9
Georgia Institute of Technology Main for Testing public static void main(String[] args) { String[] searchMe = {"apple","bear","cat","dog","elephant"}; System.out.println(binaryFind("apple",searchMe)); System.out.println(binaryFind("cat",searchMe)); System.out.println(binaryFind("giraffe",searchMe)); }

10
Georgia Institute of Technology How it Works When you search for apple –start = 0, end = 4, so checkpoint = 2 apple is less than cat so end changes to checkpoint – 1 –start = 0, end = 1, so checkpoint = 0 We find apple at index 0 and return When you search for giraffe –start = 0, end = 4, so checkpoint = 2 Giraffe is greater than cat so start changes to checkpoint + 1 –start = 3, end = 4, so checkpoint = 3 Giraffe is greater than dog so start changes to checkpoint + 1 –start = 4, end = 4, so checkpoint = 4 Giraffe is greater than elephant so start changes to checkpoint + 1 = 5 and the loop stops since start > end

11
Georgia Institute of Technology Exercise Predict the output of binaryFind with the following main method public static void main(String[] args) { String[] searchMe = {"apple","bear","cat","dog","elephant"}; System.out.println(binaryFind("bear",searchMe)); System.out.println(binaryFind("comb",searchMe)); System.out.println(binaryFind("elephant",searchMe)); }

12
Georgia Institute of Technology Summary Searching algorithms try to find an item in a collection of items –A linear search checks each item until you find the right one O(n) –A binary search takes advantage of the items being in order to cut down on the number of time you need to compare items O(log n) Best case time is the fastest an algorithm will finish, worst case is the longest, and average case is the average time

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google