rec15 - CS 177 Week 15 Recitation Slides Speed Big-O...

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

View Full Document Right Arrow Icon
1 CS 177 Week 15 Recitation Slides Speed Big-O notation Sorting and Searching and Functions with Recursion
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 ANY QUESTIONS?
Background image of page 2
More than one way to solve a problem There’s always more than one way to solve a problem. You can walk to one place around the block, or by taking a shortcut across a parking lot. Some solutions are better than others. How do you compare them? 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
How do we compare algorithms? There’s more than one way to solve a problem. How do we compare algorithms to say that one is faster than another? Computer scientists use something called Big-O notation It’s the order of magnitude of the algorithm Big-O notation tries to ignore differences between languages, even between compiled vs. interpreted, and focus on the number of steps to be executed. 4
Background image of page 4
Which one of these is more complex in Big-O notation? def increaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*1.2) def increaseVolume(sound): for sample in getSamples(sound): value = getSample(sample) setSample(sample,value * 2) 5 Neither – each one process each pixel and sample once. As the data increases in size, the amount of time increases in the same way. O(n) where n is the number of pixels in picture O(n) where n is the number of samples in sound
Background image of page 5

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

View Full DocumentRight Arrow Icon
Nested loops are multiplicative def loops(): count = 0 for x in range(1,5): for y in range(1,3): count = count + 1 print x,y,"--Ran it ",count,"times" >>> loops() 1 1 --Ran it 1 times 1 2 --Ran it 2 times 2 1 --Ran it 3 times 2 2 --Ran it 4 times 3 1 --Ran it 5 times 3 2 --Ran it 6 times 4 1 --Ran it 7 times 4 2 --Ran it 8 times 6 How many times will this statement be executed?
Background image of page 6
Big-O notation example 7 def sortSampleValues(sound): n = getLength(sound) for i in range(0, n-1): for j in range(0, n-1): value1 = getSampleValueAt(sound, j) value2 = getSampleValueAt(sound, j+1) if(value1 > value2): setSampleValueAt(sound, j, value2) setSampleValueAt(sound, j+1, value1) return sound How many times will this portion be executed? It will be executed roughly n
Background image of page 7

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

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

This note was uploaded on 02/22/2012 for the course CS 177 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 25

rec15 - CS 177 Week 15 Recitation Slides Speed Big-O...

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

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