L12cs2110fa08

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

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

Searching, Sorting, and Asymptotic Complexity Lecture 12 CS2110 – Fall 2008

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

View Full Document
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
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?

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

View Full Document
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)
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; }

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

View Full Document
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… isn’t it? How do we measure to show

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

View Full Document
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
Ask a homework question - tutors are online