L12cs2110fa08 - Searching Sorting and Asymptotic Complexity Lecture 12 CS2110 Fall 2008 Announcements Prelim 1 Thursday October 16 7:30 9pm Uris

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Searching, Sorting, and Asymptotic Complexity Lecture 12 CS2110 – Fall 2008
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Announcements Prelim 1 Thursday, October 16, 7:30 - 9pm, Uris Auditorium Topics all material up to (but not including) searching and sorting (this week’s 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:30-9pm & 9-10: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
Background image of page 2
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?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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)
Background image of page 4
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; }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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
Background image of page 6
6 Linear Search vs Binary Search Which one is better? Linear Search is easier to program But Binary Search is faster… isn’t it? How do we measure to show
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/01/2010 for the course CS 2110 at Cornell University (Engineering School).

Page1 / 27

L12cs2110fa08 - Searching Sorting and Asymptotic Complexity Lecture 12 CS2110 Fall 2008 Announcements Prelim 1 Thursday October 16 7:30 9pm Uris

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online