






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Outline
Last time:
Today:
Mirror, mirror on the wall,....
Given two algorithms, how do you determine which is more efficient?
Runtime computation: name search
Population (size) Linear Binary EECS 281 (100) 10 ms 0.7 ms (lookup 7 names) UM (40000) 4 secs 1.5 ms Washtenaw 35 secs 1.8 ms MI (9 mil) 15 mins 2.3 ms US (290 mil) 8 hours 2.8 ms World (6 billion) 7 days 3.3 ms
Assume can search 10 names/ms! An 8-year old may take longer
Roofing Problem
A builder can carry two shingles on his shoulder as he climbs up the ladder. He then climbs down and carries two more shingles up the ladder. Each round trip (up and down the ladder) costs $2.
Questions:
Answers:
What is the ONE operation we counted in this case?
Operation Count Example: Find Max
Given an array of N elements, find the index of the largest element
Questions:
Four Operation Count Accounting Rules
Rule 1: Consecutive Statements : S 1 ; S 2 ; S 3 ;... ; SN ; The runtime R of a sequence of statements is the runtime of the statement with the max runtime, max(R(S 1 ), R(S 2 ),... , R(SN )).
Rule 2: Conditional Execution : if (S 1 ) S 2 ; else S 3 ; The runtime of a conditional execution is max(R(S 1 ), R(S 2 ), R(S 3 )).
Four Accounting Rules (cont)
Rule 3: Iteration/Loop : for (S 1 ; S 2 ; S 3 )S 4 ; The runtime of a loop with n iterations is max(R(S 1 ), n ∗ R(S 2 ), n ∗ R(S 3 ), n ∗ R(S 4 )).
Rule 4: Nested Loop :
Count inside out. The runtime is the max of the runtime of each statement multiplied by the total number of times the statement is executed.
Operation Count Example: Compute Rank
Given an unsorted array a of N elements, compute an array r, where r[i] is the rank of a[i] in a.
The rank of an element is defined as the number of all elements in the array that is smaller than it, plus the number of elements to its left that is equal to it.
Example: a[4, 3, 9, 3, 7] gives r[2, 0, 4, 1, 3]
Time Complexity of rank(): how many CMPs in total?
Aside: Pseudocode