




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The lower bounds for comparison sorts, focusing on algorithms such as insertion sort, selection sort, merge sort, quicksort, and heapsort. The concept of decision trees and their properties to prove that the lower bound for comparison sorts is ω(nlgn). The document also covers non-comparison sorts like counting sort and radix sort.
Typology: Lecture notes
1 / 28
This page cannot be seen from the preview
Don't miss anything!





















Department of Computer Science and Engineering
National Taiwan Ocean University
Outline `^ Lower
2
Lower bounds for sorting `^ Lower
n ) to
examine
all^ the
input.
`^ All
sorts
seen
so^ far
are^ Ω
( n lg n
`^ We’ll
show
that
Ω( n
lg n ) is
a^ lower
bound
for^ comparison
sorts.
`^ Abstraction
of^ any
comparison
sort.
`^ A^ full
binary
tree.
`^ Represents
comparisons
made
by
`^ a^ specific
sorting
algorithm
`^ on^
inputs
of^ a^ given
size.
`^ Control,
data
movement,
and^
all^ other
aspects
of^ the
algorithm
are^ ignored. 4
`^ There
are^ ≥
n!^ leaves,
because
every
permutation
appears
at^ least
once. Decision tree^5
1:
1:
2:
1: 2: 〈1,2,3〉
〈1,3,2〉
〈3,1,2〉
〈2,1,3〉
〈2,3,1〉
〈3,2,1〉
≤ ≤
≤
≤ A [1]^ >^ A [2] ≤
A [1]^ >^
A [2] A [1]^ >^
A [3]
A [1]^ ≤^
A [2] A [2]^ >^
A [3]
A [1]^ ≤^
A [2]
A [1]^ ≤^
A [2]^ ≤^
A [3]
compare
A [1]^ to
A [2]
Properties of decision trees
2/
Ω( n
`^ l^ ≥
n !,^ where
l^ =^ #
of^ leaves.
`^ By
lemma
1,^ n!
≤^ l^ ≤
h^ 2 or
h^ 2 ≥^ n !.
`^ Take
logs:
h^ ≥^ lg(
n !).
`^ Use
Stirling’s approximation:
n!^ > (
nn/e )
h^ > lg(
nn / e ) = n lg( n / e
=^ n lg
n^ −^ n
lg^ e =^ Ω( n
lg n ). 7
Properties of decision trees
3/
`^ The
O ( n lg
n )^ upper
bounds
on^ the
running
times
for^ heapsort
and^ merge
sort^
match
the^ Ω
( n lg n
)^ worst
‐case
lower
bound
from
Theorem
8
Counting sort `^ Non
.^.^ n ],
where
A [^ j^ ]
.^ ,^ k }
for^ j
.^ ,^ n.
Array
A^ and
values
n^ and
k^ are
given
as^ parameters.
.^ n ],^
sorted.
B^ is^ assumed
to^ be
already
allocated
and
is^ given
as^ a^
parameter.
.^ k ].
10
The C
OUNTING
‐SORT
procedure
C^ OUNTING
‐SORT^ (
A ,^ B ,^ k
)
1.^
for^ i^ ←
0 to^ k
2.^
do^ C [ i
]^ ←^0
3.^
for^ j^ ←
1 to^ length
[ A ]
4.^
do^ C [ A
[ j ]]^ ←
C [ A [ j ]]
+^1
5.^
/*^ C [ i ]
now^ contains
the^ number
of^ elements
equal
to^ i.^ */
6.^
for^ i^ ←
1 to^ k
7.^
do^ C [ i
]^ ←^ C
[ i ]^ +^ C
[ i^ −^ 1]
8.^
/*^ C [ i ]
now^ contains
the^ number
of^ elements
less^ than
or^ equal
to^ i.^ */
9.^
for^ j^ ←
length
[ A ]^ downto
^1
10.^
do^ B [ C
[ A [ j ]]]
←^ A [
j ]
11.^
C [ A [ j ]]
←^ C [
A [ j ]]^ −
1
11
Θ( k ) Θ( n ) Θ( k ) Θ( n )
Properties of counting sort `^ A^ sorting
`^ Counting
sort
will^ be
used
in^ radix
sort.
13
Outline `^ Lower
14
Correctness of radix sort `^ Proof:
By^ induction
on^ number
of^ passes
( i^ in^
pseudocode).
^ Basis:^^i^ =^ 1.
There
is^ only
one^
digit,
so^ sorting
on^ that
digit
sorts
the array.
`^ Inductive
step: `^ Assume
digits
… ,^ i^ −
1 are
sorted.
`^ Show
that
a^ stable
sort^
on^ digit
i^ leaves
digits
… ,^ i^ sorted:
`^ If^2
digits
in^ position
i^ are^
different,
ordering
by^ position
i^ is^ correct,
and^ positions
1,…,^
i^ −^1 are
irrelevant.
`^ If^2
digits
in^ position
i^ are^
equal,
numbers
are^ already
in^ the
right
order
(by^ inductive
hypothesis).
The^ stable
sort^
on^ digit
i^ leaves
them
in^ the
right
order.
16
Time complexity of radix sort `^ Assume
n^ d ‐digit
numbers
in^ which
each
digit
can^
take^
on
up^ to
k^ possible
values,
correctly
sorts
these
numbers
in
Θ( d ( n
+^ k ))
time. 17
Break each key into digits
2/
`^ Balance
b / r^ and
n^ +^2
r.
`^ If^ r
lg^
n ,^ then
r^ 2 term
in^ the
numerator
increases
faster
than
the^ r
term
in^ the
denominator. `^ If^ r
< lg n
,^ then
b / r^ term
increases,
and^
n^ +^2
r^ term
remains
at^ Θ
( n ).
19
).lg ( )) ( lg (^
bn n nn b n
θ
θ^
=
The main reason `^ How
counting
sort^
allows
us^ to
gain
information
about
keys
by
means
other
than
directly
comparing
2 keys.
`^ Used
keys
as^ array
indices.
20