week 15
play

Week 15 Exceptions Linear Search Binary Search Exceptions, - PowerPoint PPT Presentation

Week 15 Exceptions, Sorting, Searching & Review Week 15 Week 15 Exceptions Linear Search Binary Search Exceptions, Sorting, Searching & Review Insert Insertion Sort Review Spring 2014 Student Responsibilities Week 15


  1. Week 15 Exceptions, Sorting, Searching & Review Week 15 Week 15 Exceptions Linear Search Binary Search Exceptions, Sorting, Searching & Review Insert Insertion Sort Review Spring 2014

  2. Student Responsibilities Week 15 Exceptions, Sorting, Reading: Textbook, Chapter 12.1 – 12.3 Searching & Review Lab is a participation lab this week – show up and get a Week 15 Exceptions finch to move, light up, and say something, then show me. Linear Search Binary Search Lab 14 is due no later than 4:00 pm Friday Insert Insertion Sort Attendance Review Study for finals : review old exams, quizzes, labs, assigned reading, and handouts — and remember to: Get some sleep!

  3. Exception Handling – a Short Intro Week 15 There are some runtime situations which cause a Java Exceptions, Sorting, program to “crash.” Examples include: Searching & Review division by zero Week 15 attempting to access an array with an index which is out of Exceptions its bounds Linear Search attempting to dereference a null pointer Binary Search Insert These types of problems are called exceptions . Insertion Sort Review When a Java method encounters a problem during execution, it responds by throwing an exception — this is the process of reporting an exceptional condition outside the normal program flow.

  4. Runtime Exceptions Week 15 Exceptions, When an exception is thrown , the Java runtime system: Sorting, Searching & stops executing code at that point Review Week 15 begins searching backwards for methods on the control Exceptions stack — starting with the current method and proceeding to the method which called it, and so forth Linear Search Binary Search until it finds a method that expresses the intent to Insert catch such an exception if one is thrown Insertion Sort Review Most runtime exceptions do not need to be caught — the exception will simply propagate backwards on the control stack, eventually causing the program to halt.

  5. try & catch() Week 15 On the other hand, attempting to access an input file which Exceptions, Sorting, doesn’t exist, for example, is a different situation. Searching & Review Week 15 Programmers who use the java.io package are required Exceptions (i.e., forced ) by Java to check for the exceptions that the Linear Search methods in that package throw. Binary Search Insert Thus, the code to open and read a file in Java is not Insertion Sort complete unless it explicitly catches exceptions propagated Review from the IOException class. The code that works with data files must appear inside a try statement, with an associated catch() statement.

  6. try - catch() Syntax try Week 15 { Exceptions, Sorting, // code in which an exception might occur Searching & Review } Week 15 Exceptions catch (type identifier) Linear Search { Binary Search // code to respond to the exception Insert } Insertion Sort Review Where: type is the type of the exception identifier is the name of a variable that hold the exception information

  7. File Opening Example Week 15 Exceptions, Sorting, Searching & try Review { Week 15 code to open and read the file Exceptions } Linear Search Binary Search catch (IOException ex) Insert { Insertion Sort code to respond to exceptions that occur Review }

  8. InputFile Class by Dr. Mertz — Examples Week 15 /** Read an entire line from the file. Returns null if an Exceptions, * error occurs or the end of the file is reached. Sorting, * @return the next line of text from the file */ Searching & Review public String readLineFromFile(){ Week 15 if (buffer == null) { // connection to file failed Exceptions showErrorDialog(NO_FILE_ERROR); Linear Search return null; } Binary Search Insert try { Insertion Sort return buffer.readLine(); Review } catch (IOException ex) { showErrorDialog(FILE_IO_ERROR); return null; } }

  9. /** Tries to read next word and convert it into an Integer. Week 15 * The converted value (or null if error) is returned. */ Exceptions, Sorting, public Integer readIntegerFromFile() { Searching & Review if (buffer == null) { // no file connected to buffer showErrorDialog(NO_FILE_ERROR); Week 15 return null; Exceptions } Linear Search String word = readWordFromFile(); Binary Search Insert if (word == null) { return null; } Insertion Sort Review try { return Integer.valueOf(word); } catch (NumberFormatException ex) { showErrorDialog(word + " is not an integer!"); return null; } }

  10. Searching Week 15 Exceptions, Given a list of items, it is often the case that we need to Sorting, Searching & search the list for a specific value. Review Week 15 One example might be finding Aunt Sally’s address among Exceptions your list of family and friends. Linear Search Binary Search Insert Something that impacts how we search is whether or not Insertion Sort the list is in order. Review Looking someone up by their last name in a phone book is different than looking for their information on scraps of paper stuffed in your backpack.

  11. Types of Searches Week 15 In a Linear search, we start at the beginning and work our Exceptions, Sorting, way through the entire list one item at a time until we either Searching & Review find what we’re looking for, or we reach the end of the list. Week 15 Linear search can be used on any list. Exceptions Linear Search Binary Search If a list is sorted, we can use a Binary search: Insert check the middle value of the list Insertion Sort If we find what we’re looking for, we’re done. Review If we don’t, then we can throw half the list away and look in the middle of the half–list we now have. This process continues until we find what we’re looking for, or the current list is empty. Binary search can only be used on sorted lists.

  12. Linear Search Algorithm Week 15 Mark the region in the list to search Exceptions, Sorting, Searching & while (the region is not empty) { Review probe the first entry of the region Week 15 if (Target is found){ Exceptions note the location return true Linear Search } Binary Search else Insert go on to the next entry, decrease the region Insertion Sort } Review if we get to this point, the region is empty yet we still have not found the target, so { set location to an impossible value return false }

  13. Linear Search: Marking a Region Week 15 Exceptions, Sorting, Searching & Review != Target Week 15 Exceptions Linear Search 0 Low L.size()−1 Binary Search Insert Insertion Sort Low began at 0. Search is partially completed. Review The cyan region to the left of Low has already been considered and does not contain the Target.

  14. Linear Search: Worst Case Number of Probes Week 15 Exceptions, Sorting, Searching & Review Week 15 Exceptions For an ArrayList of size n , Linear Search linear search makes at most n probes (comparisons). Binary Search Insert Insertion Sort Review

  15. Searching a Temperature List: Linear Search Week 15 public static boolean LinearSearch( Exceptions, ArrayList<Temperature> L, Sorting, Temperature Target, Searching & Review Integer Location){ int Low = 0; Week 15 while (Low < L.size()) { Exceptions // Target is found Linear Search if (Target.isEqual(L.get(Low)) { Binary Search Location = Low; Insert return true; Insertion Sort } Review else // Target may be in right-hand section Low++; // Discard mismatch } Location = L.size(); // Target not in list return false; }

  16. Linear Search: Integer List Example Week 15 Exceptions, Sorting, 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Searching & Review 10 14 16 20 22 26 29 32 40 42 49 51 53 62 75 Week 15 Low = 0 Target = 16 Exceptions 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Linear Search 10 14 16 20 22 26 29 32 40 42 49 51 53 62 75 Binary Search Insert Low = 1 Target = 16 Insertion Sort Review 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 10 14 16 20 22 26 29 32 40 42 49 51 53 62 75 Low = 2 Target = 16

  17. Searching a Sorted list: Binary Search Algorithm Week 15 Exceptions, Mark the region to search Sorting, Searching & Review while (the region is not empty) { Week 15 probe near the middle of the region Exceptions if (found) Linear Search note the location, return true Binary Search else Insert shrink the region by discarding Insertion Sort the lower or upper portion as appropriate Review } set location return false

  18. Searching a Sorted list: Marking a Region Week 15 Exceptions, Sorting, Searching & < Target > Target Review Week 15 0 Low High L.size()−1 Exceptions Linear Search Binary Search Insert Low began at 0, High at L.size()-1 . Insertion Sort Review Search is partially completed. The cyan regions to the left of Low and right of High have already been considered and do not contain the Target.

  19. Binary Search: Worst Case Number of Probes Week 15 For a list of size n , binary search makes at most ⌊ log 2 ( n ) ⌋ + 1 Exceptions, probes or comparisons. Sorting, Searching & Review n Linear Binary Week 15 2 2 − 1 = 3 3 2 Exceptions 2 3 − 1 = 7 7 3 Linear Search 2 4 − 1 = 15 Binary Search 15 4 Insert 2 5 − 1 = 31 31 5 Insertion Sort . . . . . . Review . . . 2 10 − 1 = 1023 1023 10 . . . . . . . . . 2 20 − 1 = 1048575 1048575 20

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend