This preview shows pages 1–3. Sign up to view the full content.
This 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 2008 2 Announcements ! Prelim 1 " Thursday, October 16, 7:30  9pm, Uris Auditorium " Topics # all material up to (but not including) searching and sorting (this weeks topics) # including interfaces & inheritance ! Exam conflicts " Email Kelly Patwell ASAP ! A3 due Friday, October 10, 11:59pm ! Prelim 1 review sessions " Wednesday 10/15, 7:309pm & 910:30pm, Upson B17 (sessions are identical) " See Exams on course website for more information " Individual appointments are available if you cannot attend the review sessions (email one TA to arrange appointment) ! Old exams are available for review on the course website 3 What Makes a Good Algorithm? ! 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? 4 Sample Problem: Searching 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; } 5 Sample Problem: Searching 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 6 Linear Search vs Binary Search ! 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 7 One Basic Step = One Time Unit ! Basic step:...
View
Full
Document
This note was uploaded on 02/01/2010 for the course CS 2110 at Cornell University (Engineering School).
 '07
 FRANCIS
 Inheritance, Sort

Click to edit the document details