{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

rec15

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

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

1 CS 177 Week 15 Recitation Slides Speed Big-O notation Sorting and Searching and Functions with Recursion

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

View Full Document
2 ANY QUESTIONS?
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

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

View Full Document
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
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

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

View Full Document
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?
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

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 / 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
Ask a homework question - tutors are online