Career research project Kevin Dombrowski. Computer Programmer.
New Mexico Computer Science For All Search Algorithms Maureen Psaila-Dombrowski.
-
Upload
ella-fields -
Category
Documents
-
view
212 -
download
0
Transcript of New Mexico Computer Science For All Search Algorithms Maureen Psaila-Dombrowski.
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
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
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
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
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
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 ?
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
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
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
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