This preview shows pages 1–8. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 Fall 2009 Announcements 2 Prelim 1 Thursday, October 15, 7:30  9pm, G01 Uris Topics all material up to (but not including) searching and sorting (this weeks topics) including interfaces & inheritance Exam conflicts A number of people will take P1 on the same day but from 6:006:30 (still Uris G01) Email me ASAP if you have a conflict but cant solve it this way! A3 due Friday, October 10, 11:59pm Review sessions: What Makes a Good Algorithm? 3 Suppose you have two possible algorithms or data structures that basically do the same thing; which is better ? Well what do we mean by better ? Faster? Less space? Easier to code? Easier to maintain? Required for homework? How do we measure time and space for an algorithm? Sample Problem: Searching 4 static boolean find(int a, int item) { for (int i = 0; i < a.length; i++) { if (a[i] == item) return true; } return false; } Determine if a sorted array of integers contains a given integer First solution: Linear Search (check each element) static boolean find(int a, int item) { for ( int x : a ) { if (x == item) return true; } return false; } Sample Problem: Searching 5 static boolean find (int a, int item) { int low = 0; int high = a.length  1; while (low <= high) { int mid = (low + high)/2; if (a[mid] < item) low = mid + 1; else if (a[mid] > item) high = mid  1; else return true; } return false; } Second solution: Binary Search Linear Search vs Binary Search 6 Which one is better? Linear Search is easier to program But Binary Search is faster isnt it? How do we measure to show that one is faster than the other Experiment? Proof? Which inputs do we use? Simplifying assumption #1: Use the size of the input rather than the input itself For our sample search problem, the input size is n+1 where n is the array size Simplifying assumption #2: Count the number of basic steps rather than computing exact times One Basic Step = One Time Unit 7 Basic step: input or output of a scalar value...
View
Full
Document
This note was uploaded on 03/08/2010 for the course CS 2110 taught by Professor Francis during the Spring '07 term at Cornell University (Engineering School).
 Spring '07
 FRANCIS
 Data Structures, Sort

Click to edit the document details