Algorithm Analysis: Searching Fall 2013 Carola Wenk Searching A - - PowerPoint PPT Presentation

algorithm analysis searching
SMART_READER_LITE
LIVE PREVIEW

Algorithm Analysis: Searching Fall 2013 Carola Wenk Searching A - - PowerPoint PPT Presentation

Algorithm Analysis: Searching Fall 2013 Carola Wenk Searching A List A very common task is to check whether an item is contained in a list. L: 2 5 1 0 8 6 -3 4 -1 ... Does L contain the number 6? 1. Define the problem (input, output)


slide-1
SLIDE 1

Algorithm Analysis: Searching

Fall 2013 Carola Wenk

slide-2
SLIDE 2

Searching A List

...

Input

A list L and an item x to check whether it is contained in L.

Output

The item’s index, if it is in the list. What should the output be if x is not in L?

  • 1. Define the problem (input, output)

A very common task is to check whether an item is contained in a list.

2 5 1

  • 1

8 6 -3 4

L: Does L contain the number 6?

slide-3
SLIDE 3

Searching A List

  • 2. Describe the algorithm

Scan the list L from left to right and compare it with

  • x. If we find x, return the index. If not, return -1.

Runtime is linear in the size of the list.  Linear search ...

A very common task is to check whether an item is contained in a list.

2 5 1

  • 1

8 6 -3 4

L: Does L contain the number 6?

slide-4
SLIDE 4

Sorted Lists

  • Sorting is constantly used, but in many applications, the data

does not change frequently and we only need to sort it once.

increasing

Where is the minimum element? The maximum element? The median? A very common task is to check whether an item is in our (typically large) list. If the list is sorted, can we do better than searching the entire list?

slide-5
SLIDE 5

Searching A Sorted List

...

minimum median maximum

Input

A list L sorted in increasing order, and an item x to check whether it is contained in L.

Output

The item’s index, if it is in the list.

  • 1 if x is not in L.
  • 1. Define the problem (input, output)
  • 3 -1 0

1 2 4 5 6 8

slide-6
SLIDE 6

Searching A Sorted List

median

By definition, half of the elements are smaller than the median and half of elements are greater. What can we do? Suppose we are looking for some element, call it x. What do we know if the median is smaller than x? If it is larger?

slide-7
SLIDE 7

Searching A Sorted List

Suppose we are looking for some element, call it x.

median

By definition, half of the elements are smaller than the median and half of the elements are greater. What can we do? What do we know if the median is smaller than x? If it is larger?

slide-8
SLIDE 8

Searching A Sorted List

Suppose we are looking for some element, call it x.

median

What do we know if the median is smaller than x? If it is larger? By inspecting the median, we can decide which half of the list to eliminate from consideration with a single comparison.

slide-9
SLIDE 9

Searching A Sorted List

Suppose we are looking for some element, call it x. What do we know if the median is smaller than x? If it is larger? By inspecting the median, we can decide which half of the list to eliminate from consideration with a single comparison.

median

slide-10
SLIDE 10

Searching A Sorted List

median

Binary Search:

  • 1. Test whether x is less than the median (if it is equal, we

are done).

  • 2. Continue to search the half of the list that x is in.
  • 3. We are done when the “correct” side of the list is empty.