W12_search_sort

# W12_search_sort - CSC108H/A08H Week 12 Searching Searching...

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

CSC108H/A08H Week 12 Searching, Searching, . ..

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

View Full Document
Searching Searching is one of the things computers do very well How do we know how long it can take to search for something? Suppose we look at the number of searches necessary to conclude that the element we're looking for is not in the list We did searching before - now let's see how we can get better and better: 2
search 3 def search(L, value): '''Return the index of value in L, or len(L) if value doesn't exist in L.''' i = 0 while i < len(L) and L[i] != value: i += 1 return i Why do we need two comparisons? Can we do better?

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

View Full Document
sentinel_search 4 def sentinel_search(L, value): '''Return the index of value in L, or len(L) if value doesn't exist in L.''' L.append(value) i = 0 while L[i] != value: i += 1 L.pop() Eliminated one comparison per iteration. Added one append and pop. How much better are we? Can we do better?
Are we substantially better? What's the complexity of search ? complexity - how performance time increase with number of elements 2 comparisons for every element of L of sentinel_search ? 1 comparison for every element Both functions: We have to go through every element if the desired element is not in the list complexity is linear : the time required is strictly proportional to the number of elements to be searched 5

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 20

W12_search_sort - CSC108H/A08H Week 12 Searching Searching...

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

View Full Document
Ask a homework question - tutors are online