Week_12_Algorithm

# Week_12_Algorithm - CMPT120 J. Ye Searching and Sorting...

This preview shows pages 1–3. Sign up to view the full content.

CMPT120 J. Ye Searching and Sorting Algorithms (Week 12) Searching Efficiency of algorithms Sorting CMPT120 J. Ye Searching is the process of finding a target element within a group of items called the search pool; the target may or may not be in the search pool. The search pool can have different data structure. Here we examine sequences (mostly we use lists) In Python, searching in a sequence can be done using in , which returns True or False but sometimes we may need the index of the item Let's look at two classic searching approaches: linear search and binary search 2 Searching

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

View Full Document
CMPT120 J. Ye A linear search begins at one end of a list and examines each element in turn Eventually, either the item is found or the end of the list is encountered Implement Linear Search function : the function takes a list of values and a target value as input the function returns true if the target value is found in the list or false otherwise; do not use the keyword in What about returning the position of the target value in the list? (if not found, return -1) 3 Linear Search Searching Let’s examine two different implementations of the linear search using three different inputs. See handout … Conclusion: It takes the longest time to complete the linear search when the target value is not in the search pool -- we call this “worst case” when talking about the efficiency of algorithms In the worst case, for a list of size n, how many elements
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 08/28/2011 for the course CMPT 125 taught by Professor Diana during the Spring '08 term at Simon Fraser.

### Page1 / 7

Week_12_Algorithm - CMPT120 J. Ye Searching and Sorting...

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

View Full Document
Ask a homework question - tutors are online