lecture15 - Lecture 15 Algorithms Algorithms Many problems...

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

View Full Document Right Arrow Icon
Lecture 15 Algorithms
Background image of page 1

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

View Full DocumentRight Arrow Icon
Algorithms Many problems involving discrete structures are solved using algorithms. An algorithm is a finite set of precise instructions for performing a computation that solves a problem Not too different from a proof
Background image of page 2
Searching Algorithms Input: a sequence of n distinct integers a 1 , a 2 , …, a n ; and an integer x Goal: to find if x is in the sequence Output: 0 if x is not in the sequence, the index of x otherwise
Background image of page 3

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

View Full DocumentRight Arrow Icon
Linear Search Algorithm procedure linear_search ( x : integer; a 1 , a 2 , …, a n : integers) location = 0 for ( i =1 to n) if a i = x then location := i break { location is the subscript of the term that equals x, or it is 0 if x is not found}
Background image of page 4
Exercise Prove that the linear search algorithm is correct
Background image of page 5

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

View Full DocumentRight Arrow Icon
What is the running time of linear_search? Well, it depends not only the size of the input sequence, but also the location of x For such cases, often we use worst case analysis How many steps will linear_search take in the worst case? Can you do better than this in general?
Background image of page 6
Binary Search When the input is sorted, we can use this structure to find a more efficient search algorithm
Background image of page 7

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

View Full DocumentRight Arrow Icon
Binary Search procedure binary_search ( x : integer; a 1 , a 2 , …, a n : increasing integers) i := 1 { i is left endpoint of search interval } j := n { j is right endpoint of search interval } while i < j begin m := ( i + j )/2 { m is the point in the middle } if x > a m then i := m +1 else j := m end if x = a i then location := i else location := 0 { location is the subscript that equals x, or it is 0 if x is not found}
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/21/2011 for the course CSCI 2011 taught by Professor Staff during the Spring '08 term at Minnesota.

Page1 / 31

lecture15 - Lecture 15 Algorithms Algorithms Many problems...

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

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