n is v.size().
(asic operation: v[i] += v[k].
Two nested ordinary loops, so (n
)
).
A1#% !! "#$ter %&st 'e a rando%(access iterator t)*e
te%*+ate <t)*ena%e "#$ter>
void 'ar("#$ter first, "#$ter +ast)
{
std::size_t size = +ast ( first;
if (size <= -)
ret&rn;
"#$ter %idd+e = first + size!.;
'ar(first, %idd+e);
'ar(%idd+e, +ast);
for ("#$ter it = first; it != +ast; ++it)
(/it) += size;
n is the number o" items in the given list.
(asic operation: (/it) += size.
*ecurrence "or number o" steps required: T(n) + )T(n,)) - n.
%se the aster Theorem. a + ), b + ), d + #.
'o a + b
d
. *esult: (n
d
log n) + (n log n).
Exam 'age 1
A4% Gaussian Elimination.
A4a% !hat is the time order of .a+ssian elimination9 :e s+re to s'eify
what yo+r ,n- means%
Two possible answers:
.or n equations in n un!nowns: (n
/
).
.or n being the number o" values in the input: (n
/,)
).
A4#% S+''ose we are doing .a+ssian elimination on a system whose first
two e5+ations #egin as follows;
)0
#
/0
)
x
/
...
10
#
)0
)
20
/
...
!e wo+ld not #e required to swa' the first two e5+ations% <owever, we
wo+ld 'ro#a#ly do the swa', anyway% !hy9 <ow does this hel' +s9
3aussian elimination is usually done with partial pivoting. This helps to avoid loss o"
signi"icance, which can lead to inaccurate results. When we do partial pivoting, we swap to get
the coe""icient with the greatest absolute value in the upper-le"t corner. 'ince 414 5 4)4, we would
swap the "irst two equations here.
A0% uic!select.
A0a% !hat 'ro#lem does the =+i3selet algorithm solve9 (>esri#e the
in'+t and o+t'+t? do not merely name the 'ro#lem%)
6uic!select solves the selection problem: 3iven a list o" orderable items and a number , return
the th smallest value. 7quivalently, return item in the list as it would be i" it were sorted.
A0#% =+i3selet has very 'oor worst4ase 'erformane% !hat an we do
a#o+t this9
6uic!select has linear-time average-case per"ormance, but quadratic-time worst-case
per"ormance. The standard solution is to !eep trac! o" the recursion depth, and switch to a
linear-time worst-case selection algorithm i" this depth e0ceeds some limit. (The usual
algorithm to switch to is (lum-.loyd-8ratt-*ivest-Tar9an selection, and the resulting algorithm
is !ntroselect.)
Exam 'age 4
:1% Solving "ecurrences. &n eah 'art, solve the given re+rrene exatly%
:1a%
x n=xn#2
for n). x#=:.
This is a "irst-order linear recurrence. 3eneral solution: x n=2 n .
8lugging in the initial condition x#=: , we get 2=: , and so =/.
'olution: x n=2 n/.
:1#%
x n=x n/ /)
for n/. x#=$. x )=$.
Note:
is the floor of k: the greatest integer less than or equal to k.
S+#stit+ting, we see that
x#=$ , x/=), x1=2 , x ):=;.
The
sol+tion sho+ld #e something li3e
x n=)log
/
n.
S+#stit+ting some more;
x#=$ , x )=$ , x/=), x 2=). x<=) ,
x;=). x:=), x==) , x1=2. & will g+ess that x n=) log
/
n.
This is easily he3ed for n=#, ). For n/ , we need to show that the
g+ess ma3es the e5+ation
x n=x n/ /)
tr+e%
x n=) log
/
n=) log
/
n/ /)=x n/ /),
and so the g+ess is orret%
Sol+tion;
x n=) log
/
n.
:1%
x n2 x n#2 xn)=$
for n). x$=/. x#=/.
This is a second-order linear recurrence. The characteristic polynomial is x
)
2 x2 , which
has 9ust one root: x=). %sing the appropriate case, we obtain the general solution:
x n=a
#
)
n
a
)
n)
n
.
8lugging in the "irst initial condition x$=/, we get
a
#
=/.
Then plugging in the second initial condition x#=/ , we get
a
)
=
/
)
.
'olution:
x n=/)
n
/
)
n )
n
.
Exam 'age 0
:2% #eap Sort. <ea' Sort re5+ires 8(n log n) time and +ses only onstant
additional s'ae% @o other sorting algorithm we have overed has these
'ro'erties (and neither, & #elieve, do any of the algorithms overed in CS 111)%
!hy, then, is <ea' Sort not the 'rimary algorithm +sed for most sorting9 Give
two different reasons.
The two primary reasons are that >eap 'ort is slower than other algorithms, and it is not stable.
?nother reason is that >eap 'ort requires random-access data, and so is not a good choice "or sorting
(say) a @in!ed @ist.
:1% "ed$%lac! Trees.
:1a% !hat is the order of the CAB> o'erations for a Aed4:la3 Tree9
?ll "our o" the A*%B operations are logarithmic-time "or a *ed-(lac! Tree.
:1#% >es'ite their s+'erior 'erformane (see 'art a), Aed4:la3 Trees are
+sed less and less for in4memory assoiative data% !hy is this9
>ash Tables are the usual choice "or in-memory associative data, since they o""er much better
average-case per"ormance than balanced search trees.
:1% A #alaned searh tree of some sort is generally onsidered to #e a
good im'lementation for an external assoiative data (e%g%, a large 3ey4
val+e dataset 3e't on external storage)% <owever, Aed4:la3 Trees are
rarely +sed for this, either% !hy not9
70ternal data is o"ten bloc-access: reading a single byte gets you the rest o" the bloc! "or no
additional cost. (-Trees, and the various variants o" these, are balanced search trees whose
per"ormance can be optimiCed "or bloc!-access data. This is done by setting the siCe o" a node
to the siCe o" a bloc! (or two bloc!s, etc.). *ed-(lac! Trees, with 9ust one item in each node,
cannot be optimiCed in this way.
Exam 'age C