6.9
A General Lower Bound for Sorting
.
Theorem h
Any algorithm that
sorts by comparisons only
must have a worst case computing time of
Ω
(
N
log
N
).
Proof:
K
0
≤
K
1
K
1
≤
K
2
K
0
≤
K
2
stop
[0,1,2]
stop
[0,2,1]
stop
[2,0,1]
T
F
T
F
K
0
≤
K
2
K
1
≤
K
2
stop
[1,0,2]
stop
[1,2,0]
stop
[2,1,0]
T
F
T
F
T
F
Decision tree for insertion sort on R
0
, R
1
, and R
2
When sorting
N
distinct
elements, there are
N
!
different
possible results
.
Thus any decision tree must
have at least
N
!
leaves
.
If the height of the tree
is
k
, then
N
!
≤
2
k
(# of
leaves in a complete
binary tree)
⇒
k
≥
log(
N
!)
Since
N
!
≥
(
N
/2)
N
/2
and
log
2
N
!
≥
(
N
/2)log
2
(
N
/2) =
Θ
(
N
log
2
N
)
Therefore
T
(
N
) =
k
≥
c
⋅
N
log
2
N
.
LEMMA 6.1
Let
T
be a binary of
depth
d
. then
T
has at most 2
d
leaves
LEMMA 6.2
A binary
tree with
L
leaves
must have depth at
least
log
L
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Sorting Large Structures
Problem:
Swapping large structures can be very much expensive.
Solution:
Add a pointer field to the structure and swap pointers instead
–
indirect sorting
.
Physically rearrange the structures at last
if it is really necessary.
list
key
table
[0]
d
0
[1]
b
1
[2]
f
2
[3]
c
3
[4]
a
4
[5]
e
5
table
4
1
3
0
5
2
The sorted list is
list [ table[0] ], list [ table[1] ], ……, list [ table[n

1] ]
Note:
Every permutation is made up of disjoint cycles.
. Example .
Table Sort
The cycle for any element
i
consist of
i
,
T
1
[
i
],…,
T
k
[
i
],
where
T
j
[
i
] =
T
[
T
j1
[
i
]],
T
0
[
i
] =
i
, and
T
k
[
i
] =
i
.
Cycle1:
This is the end of the preview. Sign up
to
access the rest of the document.
 Fall '08
 YueChen
 Sort, bucket sort

Click to edit the document details