Download presentation
Presentation is loading. Please wait.
Published byElla Fields Modified over 8 years ago
1
New Mexico Computer Science For All Search Algorithms Maureen Psaila-Dombrowski
2
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
3
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
4
Linear (Sequential) Search 50539810………36525……… Here’s how it works…. Have a list - 1000’s items long (numbers) Looking for a specific number (25) 25
5
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
6
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
7
Example: Looking for 18 15182739426199 151827 39426199 151827 18 Is 18 = 39 ? Is 18 = 18 ?
8
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
9
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
10
Comparison Number of Items Linear Search (Worst Case ) Binary Search (Worst Case) 10 4 100 7 1,000 10 10,000 14 100,000 17 1,000,000 20 1,000,000,000 30
11
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.