CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu Office hours: Thursday 12:00 PM – 2:00 PM Friday 8:30 AM – 10:30 AM OR request appointment via e-mail PROFESSIONALISM Turn off and put away electronics: cell phones pagers laptops tablets etc. © Dr. Carl Alphonce Today ROADMAP linear search finding max binary search (?) Coming up binary search © Dr. Carl Alphonce LINEAR SEARCH Is it there? © Dr. Carl Alphonce Computers are good at storing large amounts of data Search Finding a particular value in a large collection is a typical operation LINEAR SEARCH how to search for a value in an unordered collection general search General problem: determine whether a value exists in a given collection Assumption: to make things easier we assume that the collection contains Strings Approach: define a method which accepts as arguments a Collection<String> and a value of type String, and returns a boolean indicating whether the value is in the collection steps in defining method* Step 1 – stub out the method public boolean isMemberOf(String s, Collection<String> c){ return false; } Yes, ‘contains’ is a method already defined for this purpose. We are building an implementation of isMemberOf to understand how a method like contains works. * steps in defining method Step 2 – set up loop Can use any of while/for/for-each. public boolean isMemberOf(String s, Collection<String> c){ for (String x : c) { } return false; } steps in defining method Step 3 – set up test in body of loop public boolean isMemberOf(String s, Collection<String> c){ for (String x : c) { if (s.equals(x)) { } } return false; } steps in defining method Step 3 – set up test in body of loop public boolean isMemberOf(String s, Collection<String> c){ for (String x : c) { if (s.equals(x)) { return true; } } return false; } steps in defining method Step 3 – set up test in body of loop public boolean isMemberOf(String s, Collection<String> c){ for (String x : c) { if (s.equals(x)) { return true; } } return false; } BUT WHAT IF s IS null ? We don’t want to risk getting a NullPointerException at runtime! steps in defining method Step 4a – but we need to be careful, as s could be null! public boolean isMemberOf(String s, Collection<String> c){ for (String x : c) { if (s==null) { if (s == x) { return true; } } else { if (s.equals(x)) { return true; } } } return false; } steps in defining method Step 4b – another way to write the method public boolean isMemberOf(String s, Collection<String> c){ for (String x : c) { if (s==null && s == x) { return true; } if (s!=null && s.equals(x)) { return true; } } return false; } steps in defining method Step 4c – another way to write the method public boolean isMemberOf(String s, Collection<String> c){ if (s==null) { for (String x : c) { Do check whether s if (s == x) { return true; } == null just once. } } else { for (String x : c) { if (s.equals(x)) { return true; } } } return false; } checking that c is not null Step 4c – another way to write the method public boolean isMemberOf(String s, Collection<String> c){ if (c != null) { if (s == null) { for (String x : c) { if (s == x) { return true; } } } else { for (String x : c) { if (s.equals(x)) { return true; } } } } return false; } FINDING THE MAX © Dr. Carl Alphonce Visualization How can we determine the largest value in a collection of values? 0 1 2 3 4 5 6 17 34 12 -3 29 53 -18 47 7 Visualization Choose the first value from collection as our first candidate for the largest value. candidate 17 0 1 2 3 4 5 6 17 34 12 -3 29 53 -18 47 7 Visualization Compare the next value to the candidate. candidate 17 Is 34 > 17 ? 0 1 2 3 4 5 6 17 34 12 -3 29 53 -18 47 7 Visualization Since 34 is larger than 17, make 34 the candidate. candidate 17 YES! 0 1 2 3 4 5 6 17 34 12 -3 29 53 -18 47 7 Visualization The candidate is the largest value we have seen up to now. Continue with rest of list. candidate 34 0 1 2 3 4 5 6 17 34 12 -3 29 53 -18 47 7 Visualization Compare the next value to the candidate. candidate 34 Is 12 > 34 ? 0 1 2 3 4 5 6 17 34 12 -3 29 53 -18 47 7 Visualization Since 12 is NOT larger than 34, do not change the candidate. Continue with rest of list. candidate 34 No. 0 1 2 3 4 5 6 17 34 12 -3 29 53 -18 47 7 Visualization Compare the next value to the candidate. candidate 34 Is -3 > 34 ? 0 1 2 3 4 5 6 17 34 12 -3 29 53 -18 47 7 Visualization Since -3 is NOT larger than 34, do not change the candidate. Continue with rest of list. candidate 34 No. 0 1 2 3 4 5 6 17 34 12 -3 29 53 -18 47 7 Visualization Compare the next value to the candidate. candidate 34 Is 29 > 34 ? 0 1 2 3 4 5 6 17 34 12 -3 29 53 -18 47 7 Visualization Since 29 is NOT larger than 34, do not change the candidate. Continue with rest of list. candidate 34 No. 0 1 2 3 4 5 6 17 34 12 -3 29 53 -18 47 7 Visualization Compare the next value to the candidate. candidate 34 Is 53 > 34 ? 0 1 2 3 4 5 6 17 34 12 -3 29 53 -18 47 7 Visualization Since 53 is larger than 34, make 53 the candidate. Continue with rest of list. candidate YES! 34 0 1 2 3 4 5 6 17 34 12 -3 29 53 -18 47 7 Visualization The candidate is the largest value we have seen up to now. candidate 53 0 1 2 3 4 5 6 17 34 12 -3 29 53 -18 47 7 Visualization Compare the next value to the candidate. candidate 53 Is -18 > 53 ? 0 1 2 3 4 5 6 17 34 12 -3 29 53 -18 47 7 Visualization Since -18 is NOT larger than 53, do not change the candidate. Continue with rest of list. candidate 53 No. 0 1 2 3 4 5 6 17 34 12 -3 29 53 -18 47 7 Visualization Compare the next value to the candidate. candidate 53 Is 47 > 53 ? 0 1 2 3 4 5 6 17 34 12 -3 29 53 -18 47 7 Visualization Since 47 is NOT larger than 53, do not change the candidate. Continue with rest of list. candidate 53 No. 0 1 2 3 4 5 6 17 34 12 -3 29 53 -18 47 7 Visualization Since we have now considered each item in the collection we know the largest value in the collection is our candidate, 53. candidate 53 0 1 2 3 4 5 6 17 34 12 -3 29 53 -18 47 7 finding the max General problem: determine the maximum value in a collection, such as an ArrayList To make things easier we assume two things: that the collection contains non-null Integers that the collection is not empty Approach: define a method which accepts as argument an ArrayList<Integer> and returns the largest value is in the collection ASIDE: wrapper types Collections can hold only references to objects. Values of primitive types cannot be put into collections directly. Each primitive type has an associated wrapper class: primitive type wrapper class byte Byte short Short int Integer long Long char Character float Float double Double boolean Boolean ASIDE: wrapper types Each instance of a wrapper class holds a primitive value. Examples: int i = 3; Integer wi = new Integer(i); int y = wi.intValue(); // create wrapper // get int value double d = 3.5; Double wd = new Double(d); double z = wd.doubleValue(); // create wrapper // get double value ASIDE: autoboxing The compiler will insert code to wrap(box)/unwrap(unbox) primitive values as needed Examples: int i = 3; Integer wi = i; int y = wi; // primitive // autoboxing (wrap int) // unwrap Integer Integer z = y + wi; // unwrap and wrap Integer z = new Integer(y + wi.intValue()); steps in defining method Step 1 – stub out the method public Integer maximum(ArrayList<Integer> c){ return 0; } Exercise Write this method on your own. steps in defining method Step 1 – stub out the method public Integer maximum(ArrayList<Integer> c){ return 0; } steps in defining method Step 2 – Take first value of collection as a candidate for the maximum value. Extract first value using an iterator. public Integer maximum(ArrayList<Integer> c){ Integer candidate = c.get(0); // if c is not empty, this is safe // FIND THE MAXIMUM, STORING IT IN ‘candidate’ return candidate; } // return the candidate steps in defining method Step 3 – set up iterator-controlled loop to look at all values in collection public Integer maximum(ArrayList<Integer> c){ Integer candidate = c.get(0); // if c is not empty, this is safe for (int i=1; i < c.size(); i=i+1) { // FIND THE MAX } return candidate; } // return the candidate steps in defining method Step 5 – if it’s larger than current candidate, update candidate public Integer maximum(ArrayList<Integer> c){ Integer candidate = c.get(0); // if c is not empty, this is safe for (int i=1; i < c.size(); i=i+1) { if ( c.get(i) > candidate ) { // if we found a larger value candidate = c.get(i); // update candidate } } return candidate; } // after loop completes, candidate contains // the largest value in the collection Another solution: using a foreach loop public Integer maximum(ArrayList<Integer> c){ Integer candidate = c.get(0); // if c is not empty, this is safe for (int x : c) { if ( x > candidate ) { // if we found a larger value candidate = x; // update candidate } } return candidate; } // after loop completes, candidate contains // the largest value in the collection