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

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

View Full Document Right Arrow Icon
CSC108H/A08H Week 12 Searching, Searching, . ..
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 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?
Background image of page 3

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

View Full DocumentRight Arrow Icon
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?
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 20

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

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online