Speed part 5 Barb Ericson Georgia Institute of
Speed part 5 Barb Ericson Georgia Institute of Technology May 2006 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 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 Georgia Institute of Technology
Linear Search on Strings public static String linear. Find(String target, String[] list) { for (int index=0; index < list. length; index++) { if (target. compare. To(list[index]) == 0) { return("Found it at " + index); } } return("Not found"); } Georgia Institute of Technology
Main for Testing public static void main(String[] args) { String[] search. Me = {"apple", "bear", "cat", "dog", "elephant"}; System. out. println(linear. Find("apple", search. Me)); System. out. println(linear. Find("cat", search. Me)); System. out. println(linear. Find("giraffe", search. Me)); } 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? 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 Georgia Institute of Technology
Binary Search on Strings public static String binary. Find(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. compare. To(list[checkpoint]) == 0) { return "Found it!"; } else if (target. compare. To(list[checkpoint]) > 0) { start=checkpoint + 1; } else if (target. compare. To(list[checkpoint]) < 0) { end=checkpoint - 1; } } return "Not found"; } Georgia Institute of Technology
Main for Testing public static void main(String[] args) { String[] search. Me = {"apple", "bear", "cat", "dog", "elephant"}; System. out. println(binary. Find("apple", search. Me)); System. out. println(binary. Find("cat", search. Me)); System. out. println(binary. Find("giraffe", search. Me)); } 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 Georgia Institute of Technology
Exercise • Predict the output of binary. Find with the following main method public static void main(String[] args) { String[] search. Me = {"apple", "bear", "cat", "dog", "elephant"}; System. out. println(binary. Find("bear", search. Me)); System. out. println(binary. Find("comb", search. Me)); System. out. println(binary. Find("elephant", search. Me)); } 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 Georgia Institute of Technology
- Slides: 12