Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more ➡
Standard view
Full view
of .
×
0 of .
Results for:
P. 1
Quick Sort

# Quick Sort

Ratings: 0|Views: 2,023|Likes:

### Availability:

See More
See less

02/27/2012

pdf

text

original

Quick-Sort1
Quick-Sort
7 4 962
2 467 9 42
2479
792
29
9
Quick-Sort2
Quick-sort (§4.3)
Algorithm
Partition step
Quick-sort tree
Execution example
Analysis of quick-sort (4.3.1)
In-place quick-sort (§4.8)
Summary of sorting algorithms
Quick-Sort3
Quick-Sort
Quick-sortis a randomizedsorting algorithm basedon the divide-and-conquerparadigm:
Divide: pick a randomelement
x
(calledpivot) andpartition
S
into
L
elements less than
x
E
elements equal
x
elements greater than
x
Recur: sort
L
and
Conquer: join
L
,
E

and
x  x  L E  x
Quick-Sort4
Partition
We partition an inputsequence as follows:
We remove, in turn, eachelement
y
from
S
and
We insert
y
into
L
,
E

or
,depending on the result of the comparison with thepivot
x
Each insertion and removalis at the beginning or at theend of a sequence, andhence takes
O
(1)
time
Thus, the partition step of quick-sort takes
O
(
n
)
time
Algorithm

partition
(
S,

p
)
Input
sequence
S
, position
p
of pivot
Output
subsequences
L,

E, G
of theelements of
S
less than, equal to,or greater than the pivot, resp.
L,

E, G

empty sequences
x
S.remove
(
p
)
while

¬
S.isEmpty
()
y
S.remove
(
S.first
())
if

y
<
x  L.insertLast
(
y
)
else if
y
=
x

E.insertLast
(
y
)
else
{
y
>
x
}
G.insertLast
(
y
)
return
L,

E, G
Quick-Sort5
Quick-Sort Tree
An execution of quick-sort is depicted by a binary tree
Each node represents a recursive call of quick-sort and stores
Unsorted sequence before the execution and its pivot
Sorted sequence at the end of the execution
The root is the initial call
The leaves are calls on subsequences of size 0 or 1
7 4 962
2 467 9 42
2479
792
29
9
Quick-Sort6
Execution Example
Pivot selection
7 2 9 4
2 4 7 92
27 2 9 4 3 761
1 2 3 4 6 7 8 93 8 6 1
1 3 8 63
38
89 4
4 99
94
4

## Activity (13)

### Showing

AllMost RecentReviewsAll NotesLikes