The University of Texas at Austin
Lecture 4
Department of Computer Sciences
Professor Vijaya Ramachandran
Quicksort; basic probability
CS357: ALGORITHMS, Spring 2006
Quicksort
Quicksort
(
A, p, r
)
Input.
An array
A
[1
..n
] of elements from a totally ordered set;
p
and
r
are integers with
1
≤
p
≤
r
≤
n
.
Output.
The elements in subarray
A
[
p..r
] are rearranged in sorted order; the elements in
array
A
outside of subarray
A
[
p..r
] are unchanged.
if
p<r
then
q
:=
Partition
(
A, p, r
)
Quicksort
(
A, p, q

1)
Quicksort
(
A, q
+1
,r
)
ﬁ
To sort the entire array
A
[1
..n
]weca
l
l
Quicksort
(
A,
1
,n
).
Partition
(
A, p, r
)
Let
x
be the element in
A
[
r
]when
Partition
is called;
x
is called the
pivot element
.
Partition
(
A, p, r
) returns an index
q
,
p
≤
q
≤
r
and rearranges the elements in subarray
A
[
p..r
] such that the following properties hold after execution:
•
A
[
q
]:=
x
•
For
p
≤
i<q
,
A
[
i
]
≤
x
•
For
q<i
≤
r
,
A
[
i
]
>x
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.
 Spring '06
 Ramachandran
 Algorithms, Sort, Probability theory, expected running time

Click to edit the document details