algorithms in a nutshell
play

AlgorithmsinaNutshell Session2 Sorting 9:4010:30 Outline - PowerPoint PPT Presentation

AlgorithmsinaNutshell Session2 Sorting 9:4010:30 Outline SortingPrinciples Themes DivideandConquer Spacevs.Time Arraysvs.Pointers Comparisonvs.noncomparison


  1. Algorithms
in
a
Nutshell Session
2 Sorting 9:40
–
10:30

  2. Outline • Sorting
Principles • Themes – Divide
and
Conquer – Space
vs.
Time – Arrays
vs.
Pointers – Comparison
vs.
non‐comparison • Algorithms – QUICKSORT,
HEAPSORT,
BUCKET
SORT • Domains – Integers,
Strings,
Complex
Records Algorithms
in
a
Nutshell (c)
2009,
George
Heineman 2

  3. Sorting
Principle:
Comparison • Comparing
elements
e 1 
and
e 2 
only
one
of
the following
is
true 1. e 1 <e 2 32‐bit
int
comparison:
O(1)
constant
time
operation 2. e 1 =e 2 n‐byte
String
comparison:
O(n)
 3. e 1 >e 2 • Operation
may
be
costly
depending
upon representation – Sort
molecules
by
number
of
carbon
atoms – Compare
CH 3 COCH 2 Br
with
C 2 H 8 Algorithms
in
a
Nutshell (c)
2009,
George
Heineman 3

  4. Sorting
Principle:
Swapping • Swap
location
of
two
elements tmp
=
ar[i] ar[i]
=
ar[j] – Fundamental
operation ar[j]
=
tmp – Assumes
random
access
to
any
individual
element • Shift
two
or
more
elements – Suitable
for
arrays void
*memmove(&dest,
&src,
n) • Swapping
is
often
the
dominant
cost
of sorting – Algorithms
seek
to
reduce
wasted
swaps Algorithms
in
a
Nutshell (c)
2009,
George
Heineman 4

  5. Swapping
Example • INSERTION
SORT
Worst
Case Only 10
swaps t s r q p o n m l k j i h g f e d c b a are
really … … needed! • Every
element
swapped
maximum
#
of
times t s r q p o n m – n(n‐1)/2
=
19*20/2
=
190 s t r q p o n m – O(n 2 )
number
of
swaps r s t q p o n m • Can
we
avoid
such
situations? q r s t p o n m … Algorithms
in
a
Nutshell (c)
2009,
George
Heineman 5

  6. Divide
and
Conquer • Common
computer
science
technique • Break
up
a
problem
into
smaller
parts – Solve
each
independently INSERTION
SORT
 t s r q p o n m Note
how
each
successive
pass
through INSERTION
SORT
actually
solves
larger s t r q p o n m problems r s t q p o n m Not
much
dividing! q r s t p o n m p q r s t o n m • 
Makes
n–1
iterations o p q r s t n m n o p q r s t m m n o p q r s t Algorithms
in
a
Nutshell (c)
2009,
George
Heineman 6

  7. Divide
and
Conquer • Common
computer
science
technique • Break
up
a
problem
into
smaller
parts QUICKSORT
 t s r q p o n m Note
how
each
successive
pass
through QUICKSORT
divides
a
problem
into
two partition problems
that
are
about
half
as
big m o n p t s r q partition Solve
each
sub‐problem,
recursively m n o p q r t s • 
Makes
log 2 (n)
iterations m n o p q r s t Algorithms
in
a
Nutshell (c)
2009,
George
Heineman 7

  8. Recursion:
An
Aside • Define
a
solution
to
a
problem
using
that same
solution
as
a
sub‐step • Common
examples Base
Cases – Fibonacci
Series:

F n 
=
F n‐1 
+
F n‐2 
where


F 0
 =
F 1
 =
1 How
deep
is F 4
 =
F 3
 +
F 2 the
recursion? 2 F 3
 =
F 2
 +
F 1 5 F 2
 =
F 1
 +
F 0 int fib( int n) { n–2
levels 23 if (n == 0 || n == 1) { return 1; F 2
 =
F 1
 +
F 0 1 1 1 } return fib(n-1) + fib(n-2); 1 1 } Algorithms
in
a
Nutshell (c)
2009,
George
Heineman 8

  9. 79 QUICKSORT sort 
(A) 1. quickSort
(A,
0,
n–1) Best
case Average
case Worst
case end O(n
log
n) O(n
log
n) O(n 2 ) quickSort 
(A,
left,
right) 1. if 
(left
<
right)
 then pivot 2. pi
=
partition
(A,
left,
right) 5 6 1 3 4 2 7 3. quickSort
(A,
left,
pi–1) 4. quickSort
(A,
pi+1,
right) left
 pi right
 end A 1 3 4 2 5 6 7 • Partition Recursively Recursively sort
smaller sort
smaller – selects
an
element
to
be
 pivot sub‐array sub‐array – divides
array
into
left
and
right
sub‐arrays • Recursion – Base
Case:
No
need
to
sort
sub‐array
that
is
either
empty
or
has
a single
element:
left
≥
right – How
deep:
log( n )
on
average,
but
worst‐case
 n –1 Algorithms
in
a
Nutshell (c)
2009,
George
Heineman 9

  10. 79 QUICKSORT
Fact
Sheet left
 pi right
 • Partition 1 3 4 2 5 6 7 – selects
an
element
to
be
 pivot Elements
all
≤
pivot Elements
all
≥
pivot – divides
array
into
left
and
right
sub‐arrays QUICKSORT Algorithm Divide
and Base
Case Recursion Conquer Best
case Average
case Worst
case No
need
to
sort
sub‐array Array O(n
log
n) O(n
log
n) O(n 2 ) that
is
either
empty
or
has
a single
element:

left≥right sort 
(A) A pivot 5 6 1 3 4 2 7 1. quickSort
(A,
0,
n–1) left
 pi right
 end How
deep
is
recursion? 1 3 4 2 5 6 7 quickSort 
(A,
left,
right) Best
case:
log
(n) 1. if 
(left
<
right)
 then Recursively Recursively sort
smaller sort
smaller Worst
case:
n–1 2. pi
=
partition
(A,
left,
right) sub‐array sub‐array 3. quickSort
(A,
left,
pi–1) 4. quickSort
(A,
pi+1,
right) end Algorithms
in
a
Nutshell (c)
2009,
George
Heineman 10

  11. 79 Partition partition 
(A,
left,
right) 1. p
=
select
pivot
in
A[left,
right] 2. swap
A[p]
and
A[right] Best
case Average
case Worst
case 3. store
=
left O(n) O(n) O(n) 4. for
 i
=
left
 to 
right–1
 do 5. if 
(A[i]
 ≤ 
A[right])
 then left
 p right
 6. swap
A[i]
and
A[store] 7. store++ 5 6 1 3 4 2 7 8. swap
A[store]
and
A[right] store
 9. return 
store end 7 6 1 3 4 2 5 1 6 7 3 4 2 5 Select
a
“pivot”
value 1 3 7 6 4 2 5 • Any
value
in
array
will
do • Best
case
is
when
the
pivot
value
evenly
splits
the
array 1 3 4 6 7 2 5 Scan
left
to
right
to
find
values
less
than
pivot 1 3 4 2 7 6 5 • Swap
values
to
ensure
that
all
elements
to
the
left
of “pivot”
are
≤
to
its
value 1 3 4 2 5 6 7 Algorithms
in
a
Nutshell (c)
2009,
George
Heineman 11

  12. 79 Partition
Fact
Sheet Partition Algorithm Select
a
“pivot”
value Array • Any
value
will
do Best
case Average
case Worst
case Best
case
is
when
the • O(n) O(n) O(n) pivot
value
evenly
splits left
 p right
 partition 
(A,
left,
right) the
array 1. p
=
select
pivot
in
A[left,
right] 2. swap
A[p]
and
A[right] 5 6 1 3 4 2 7 store
 3. store
=
left Scan
left
to
right
to
find 4. for
 i
=
left
 to 
right–1
 do values
less
than
pivot 7 6 1 3 4 2 5 5. if 
(A[i]
 ≤ 
A[right])
 then Swap
values
to
ensure i
=2 • 6. swap
A[i]
and
A[store] that
all
elements
to
the 1 6 7 3 4 2 5 7. store++ i
=3 left
of
“pivot”
are
≤
to 8. swap
A[store]
and
A[right] i
=4 9. return 
store 1 3 7 6 4 2 5 its
value end i
=5 1 3 4 6 7 2 5 final 1 3 4 2 7 6 5 1 3 4 2 5 6 7 Algorithms
in
a
Nutshell (c)
2009,
George
Heineman 12

  13. Code
Check • Show
actual
running
code – Handout – Debug
example Algorithms
in
a
Nutshell (c)
2009,
George
Heineman 13

  14. 84 QUICKSORT
Optimizations • Performance,
on
average,
will
be
O( n
log
n ) – Can
still
secure
some
efficiencies • Select
Pivot – First
or
last – Random
element – Median‐of‐k
(select
median
of
 k 
elements) • Use
INSERTION
SORT
for
small
sub‐arrays – Improves
base
case
performance Algorithms
in
a
Nutshell (c)
2009,
George
Heineman 14

  15. INSERTION
SORT
vs.
QUICKSORT • INSERTION
SORT
outperforms
on
small
arrays • QUICKSORT
benefits
from
using
INSERTION SORT
on
small
sub‐arrays Algorithms
in
a
Nutshell (c)
2009,
George
Heineman 15

Recommend


More recommend