11
New Mexico Computer Science For All Search Algorithms Maureen Psaila-Dombrowski

New Mexico Computer Science For All Search Algorithms Maureen Psaila-Dombrowski

Embed Size (px)

Citation preview

Page 1: New Mexico Computer Science For All Search Algorithms Maureen Psaila-Dombrowski

New Mexico Computer Science For All

Search Algorithms

Maureen Psaila-Dombrowski

Page 2: New Mexico Computer Science For All Search Algorithms Maureen Psaila-Dombrowski

Search Algorithms

•Searching is what we when we want to find specific item among a group of items.

•Computer Science Problem You have a list You want to find a specific item - specify it Use a Search Algorithm to find it

•Two algorithms Linear (Sequential) Search Binary Search

Page 3: New Mexico Computer Science For All Search Algorithms Maureen Psaila-Dombrowski

Linear (Sequential) Search

•Simplest search method

•Method: Check every one of its elements, one at a time and in sequence, until the desired one is found

•Psuedocode ▫Specify the item you are looking for▫For each item in the list:

Check list item to see if it is desired item YES stop the search and return the item's

location. NO go to the next item on the list

Page 4: New Mexico Computer Science For All Search Algorithms Maureen Psaila-Dombrowski

Linear (Sequential) Search

50 539

810

… … … 365

25 … … …

•Here’s how it works…. Have a list - 1000’s items long (numbers) Looking for a specific number (25)

25

Page 5: New Mexico Computer Science For All Search Algorithms Maureen Psaila-Dombrowski

Linear (Sequential) Search

•Pros▫Simple easy to understand and

implement▫Works well for

Small lists Single searches of unsorted lists

•Cons▫More time consuming than other methods▫The worst case search number items in

the list

Page 6: New Mexico Computer Science For All Search Algorithms Maureen Psaila-Dombrowski

Binary Search•Problem is the same

▫Performed on sorted lists. List is already arranged in some order

Lower to highest Brightest to darkest …..

•Method: Start with a sorted list Repeat

Divide list in half Check to see which half your item is in Select that half

Page 7: New Mexico Computer Science For All Search Algorithms Maureen Psaila-Dombrowski

Example: Looking for 18

1 5 18 27 39 42 61 99

1 5 18 27 39 42 61 99

1 5 18 27

18

Is 18 <= 27 or >= 39 ?

Is 18 <= 5 or >= 18 ?

Page 8: New Mexico Computer Science For All Search Algorithms Maureen Psaila-Dombrowski

Video Clip

Malan, David J. (2013, September 7). Introduction to Computer Science, [Part of CS50/Week 0: Wednesday]. Retrieved from

http://www.youtube.com/watch?v=FWLeB436j1o

Page 9: New Mexico Computer Science For All Search Algorithms Maureen Psaila-Dombrowski

Binary Search

•Pros▫Powerful▫Fast ▫Relatively easy to understand▫Works well for large sorted data sets

•Cons▫Must sort your list first▫More difficult to implement

Page 10: New Mexico Computer Science For All Search Algorithms Maureen Psaila-Dombrowski

Comparison

Number of Items

Linear Search(Worst Case )

Binary Search

(Worst Case)

10 10 4

100 100 7

1,000 1,000 10

10,000 10,000 14

100,000 100,000 17

1,000,000 1,000,000 20

1,000,000,000 1,000,000,000 30

Page 11: New Mexico Computer Science For All Search Algorithms Maureen Psaila-Dombrowski

Summary• Search Algorithms to find a item with specific

properties among a group of items

• Linear (Sequential) Search – check every element, one at a time.

Simple easy to understand and implement Good for small lists or single searches of unsorted data More time consuming - the worst case search =

number items in the list.

• Binary Search – keep dividing the list in half and checking to see which half list your item is in until you find it!

Powerful and fast Works REALLY well for large sorted data Must sort you list first and more difficult to implement