CMPT 307 — Data Structures and Algorithms
Outline Solutions to Exercises on Sorting
1.
Show that the worstcase running time of HeapifyUp on a heap of size
n
is
Ω(log
n
)
.
If the new value added to the very end of the heap is smaller than any other element of the heap, then Heapify
Up will move it to the root of the heap. Since the length of rootto leaf paths in a heap is at least
log
n

1
,
HeapifyUp perfoms
log
n

1
swaps of elements that takes
Ω(log
n
)
time.
2.
(a) What is the running time of Quicksort whe all elements of array
A
have the same value?
(b) Show that the running time of Quicksort is
Θ(
n
2
)
when the array
A
contains distinct elements and is
ordered in decreasing order.
(a) The Partition procedure splits the array into two parts, one of which, the upper oner, is always empty. More
precisely, given a subarray
A
[
p . . . r
]
of equal elements, produces empty partition in
A
[
q
+ 1
. . .r

1]
, puts
the pivot (originally in
A
[
r
]
) into
A
[
r
]
, and produces a partition
A
[
p . . .r

1]
with only one fewer element
than
A
[
p . . . r
]
. Therefore on each recursive call the size of the array is decreased only by 1. So the recurrence
relation for Quicksort becomes
T
(
n
) =
T
(
n

1) + Θ(
n
)
, which has the solution
T
(
n
) = Θ(
n
2
)
.
(b) The Partition procedure splits the array into two parts, one of which, the lower oner, is always empty.
More precisely, given a subarray
A
[
p . . . r
]
of distinct elements in decreasing order, produces empty partition in
A
[
p . . .q

1]
, puts the pivot (originally in
A
[
r
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview. Sign up
to
access the rest of the document.
 Fall '09
 A.BULATOV
 Algorithms, Data Structures, Sort

Click to edit the document details