<s>
External	B-Algorithm
sorting	I-Algorithm
is	O
a	O
class	O
of	O
sorting	B-Algorithm
algorithms	O
that	O
can	O
handle	O
massive	O
amounts	O
of	O
data	O
.	O
</s>
<s>
External	B-Algorithm
sorting	I-Algorithm
is	O
required	O
when	O
the	O
data	O
being	O
sorted	O
do	O
not	O
fit	O
into	O
the	O
main	O
memory	O
of	O
a	O
computing	O
device	O
(	O
usually	O
RAM	B-Architecture
)	O
and	O
instead	O
they	O
must	O
reside	O
in	O
the	O
slower	O
external	B-Application
memory	I-Application
,	O
usually	O
a	O
disk	B-Device
drive	I-Device
.	O
</s>
<s>
Thus	O
,	O
external	B-Algorithm
sorting	I-Algorithm
algorithms	O
are	O
external	B-Application
memory	I-Application
algorithms	I-Application
and	O
thus	O
applicable	O
in	O
the	O
external	B-Application
memory	I-Application
model	I-Application
of	O
computation	O
.	O
</s>
<s>
External	B-Algorithm
sorting	I-Algorithm
algorithms	O
generally	O
fall	O
into	O
two	O
types	O
,	O
distribution	O
sorting	B-Algorithm
,	O
which	O
resembles	O
quicksort	B-Algorithm
,	O
and	O
external	B-Algorithm
merge	I-Algorithm
sort	I-Algorithm
,	O
which	O
resembles	O
merge	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
External	B-Algorithm
merge	I-Algorithm
sort	O
typically	O
uses	O
a	O
hybrid	B-Algorithm
sort-merge	O
strategy	O
.	O
</s>
<s>
In	O
the	O
sorting	B-Algorithm
phase	O
,	O
chunks	O
of	O
data	O
small	O
enough	O
to	O
fit	O
in	O
main	O
memory	O
are	O
read	O
,	O
sorted	O
,	O
and	O
written	O
out	O
to	O
a	O
temporary	O
file	O
.	O
</s>
<s>
External	B-Algorithm
sorting	I-Algorithm
algorithms	O
can	O
be	O
analyzed	O
in	O
the	O
external	B-Application
memory	I-Application
model	I-Application
.	O
</s>
<s>
In	O
this	O
model	O
,	O
a	O
cache	B-General_Concept
or	O
internal	O
memory	O
of	O
size	O
and	O
an	O
unbounded	O
external	B-Application
memory	I-Application
are	O
divided	O
into	O
blocks	O
of	O
size	O
,	O
and	O
the	O
running	O
time	O
of	O
an	O
algorithm	O
is	O
determined	O
by	O
the	O
number	O
of	O
memory	O
transfers	O
between	O
internal	O
and	O
external	B-Application
memory	I-Application
.	O
</s>
<s>
Like	O
their	O
cache-oblivious	B-Application
counterparts	O
,	O
asymptotically	B-General_Concept
optimal	I-General_Concept
external	B-Algorithm
sorting	I-Algorithm
algorithms	O
achieve	O
a	O
running	O
time	O
(	O
in	O
Big	O
O	O
notation	O
)	O
of	O
.	O
</s>
<s>
One	O
example	O
of	O
external	B-Algorithm
sorting	I-Algorithm
is	O
the	O
external	B-Algorithm
merge	I-Algorithm
sort	O
algorithm	O
,	O
which	O
is	O
a	O
K-way	B-Algorithm
merge	I-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
It	O
sorts	O
chunks	O
that	O
each	O
fit	O
in	O
RAM	B-Architecture
,	O
then	O
merges	O
the	O
sorted	O
chunks	O
together	O
.	O
</s>
<s>
The	O
algorithm	O
first	O
sorts	O
items	O
at	O
a	O
time	O
and	O
puts	O
the	O
sorted	O
lists	O
back	O
into	O
external	B-Application
memory	I-Application
.	O
</s>
<s>
For	O
example	O
,	O
for	O
sorting	B-Algorithm
900	O
megabytes	O
of	O
data	O
using	O
only	O
100	O
megabytes	O
of	O
RAM	B-Architecture
:	O
</s>
<s>
Read	O
100	O
MB	O
of	O
the	O
data	O
in	O
main	O
memory	O
and	O
sort	O
by	O
some	O
conventional	O
method	O
,	O
like	O
quicksort	B-Algorithm
.	O
</s>
<s>
Write	O
the	O
sorted	O
data	O
to	O
disk	B-Device
.	O
</s>
<s>
This	O
is	O
the	O
key	O
step	O
that	O
makes	O
external	B-Algorithm
merge	I-Algorithm
sort	O
work	O
externally	O
—	O
because	O
the	O
merge	O
algorithm	O
only	O
makes	O
one	O
pass	O
sequentially	O
through	O
each	O
of	O
the	O
chunks	O
,	O
each	O
chunk	O
does	O
not	O
have	O
to	O
be	O
loaded	O
completely	O
;	O
rather	O
,	O
sequential	O
parts	O
of	O
the	O
chunk	O
can	O
be	O
loaded	O
as	O
needed	O
.	O
</s>
<s>
The	O
sort	O
ends	O
with	O
a	O
single	O
k-way	O
merge	O
,	O
rather	O
than	O
a	O
series	O
of	O
two-way	O
merge	O
passes	O
as	O
in	O
a	O
typical	O
in-memory	O
merge	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
This	O
is	O
because	O
each	O
merge	O
pass	O
reads	O
and	O
writes	O
every	O
value	O
from	O
and	O
to	O
disk	B-Device
,	O
so	O
reducing	O
the	O
number	O
of	O
passes	O
more	O
than	O
compensates	O
for	O
the	O
additional	O
cost	O
of	O
a	O
k-way	O
merge	O
.	O
</s>
<s>
Eventually	O
,	O
the	O
reads	O
become	O
so	O
small	O
that	O
more	O
time	O
is	O
spent	O
on	O
disk	B-Device
seeks	O
than	O
data	O
transfer	O
.	O
</s>
<s>
A	O
typical	O
magnetic	O
hard	B-Device
disk	I-Device
drive	I-Device
might	O
have	O
a	O
10ms	O
access	O
time	O
and	O
100MB/s	O
data	O
transfer	O
rate	O
,	O
so	O
each	O
seek	O
takes	O
as	O
much	O
time	O
as	O
transferring	O
1MB	O
of	O
data	O
.	O
</s>
<s>
Thus	O
,	O
for	O
sorting	B-Algorithm
,	O
say	O
,	O
50GB	O
in	O
100MB	O
of	O
RAM	B-Architecture
,	O
using	O
a	O
single	O
500-way	O
merge	O
pass	O
is	O
n't	O
efficient	O
:	O
we	O
can	O
only	O
read	O
100MB	O
/	O
501	O
≈	O
200KB	O
from	O
each	O
chunk	O
at	O
once	O
,	O
so	O
5/6	O
of	O
the	O
disk	B-Device
's	O
time	O
is	O
spent	O
seeking	O
.	O
</s>
<s>
Then	O
the	O
sorting	B-Algorithm
process	O
might	O
look	O
like	O
this	O
:	O
</s>
<s>
Run	O
the	O
initial	O
chunk-sorting	O
pass	O
as	O
before	O
to	O
create	O
500×100MB	O
sorted	O
chunks	O
.	O
</s>
<s>
Although	O
this	O
requires	O
an	O
additional	O
pass	O
over	O
the	O
data	O
,	O
each	O
read	O
is	O
now	O
4MB	O
long	O
,	O
so	O
only	O
1/5	O
of	O
the	O
disk	B-Device
's	O
time	O
is	O
spent	O
seeking	O
.	O
</s>
<s>
Variations	O
include	O
using	O
an	O
intermediate	O
medium	O
like	O
solid-state	B-Device
disk	I-Device
for	O
some	O
stages	O
;	O
the	O
fast	O
temporary	O
storage	O
need	O
n't	O
be	O
big	O
enough	O
to	O
hold	O
the	O
whole	O
dataset	O
,	O
just	O
substantially	O
larger	O
than	O
available	O
main	O
memory	O
.	O
</s>
<s>
Repeating	O
the	O
example	O
above	O
with	O
1	O
GB	O
of	O
temporary	O
SSD	B-Device
storage	O
,	O
the	O
first	O
pass	O
could	O
merge	O
10×100MB	O
sorted	O
chunks	O
read	O
from	O
that	O
temporary	O
space	O
to	O
write	O
50x1GB	O
sorted	O
chunks	O
to	O
HDD	O
.	O
</s>
<s>
The	O
high	O
bandwidth	O
and	O
random-read	O
throughput	O
of	O
SSDs	B-Device
help	O
speed	O
the	O
first	O
pass	O
,	O
and	O
the	O
HDD	O
reads	O
for	O
the	O
second	O
pass	O
can	O
then	O
be	O
2MB	O
,	O
large	O
enough	O
that	O
seeks	O
will	O
not	O
take	O
up	O
most	O
of	O
the	O
read	O
time	O
.	O
</s>
<s>
SSDs	B-Device
can	O
also	O
be	O
used	O
as	O
read	O
buffers	O
in	O
a	O
merge	O
phase	O
,	O
allowing	O
fewer	O
larger	O
reads	O
(	O
20MB	O
reads	O
in	O
this	O
example	O
)	O
from	O
HDD	O
storage	O
.	O
</s>
<s>
Given	O
the	O
lower	O
cost	O
of	O
SSD	B-Device
capacity	O
relative	O
to	O
RAM	B-Architecture
,	O
SSDs	B-Device
can	O
be	O
an	O
economical	O
tool	O
for	O
sorting	B-Algorithm
large	O
inputs	O
with	O
very	O
limited	O
memory	O
.	O
</s>
<s>
Like	O
in-memory	O
sorts	O
,	O
efficient	O
external	B-Algorithm
sorts	I-Algorithm
require	O
O(n log n )	O
time	O
:	O
exponentially	O
growing	O
datasets	O
require	O
linearly	O
increasing	O
numbers	O
of	O
passes	O
that	O
each	O
take	O
O(n )	O
time	O
.	O
</s>
<s>
Under	O
reasonable	O
assumptions	O
at	O
least	O
500GB	O
of	O
data	O
stored	O
on	O
a	O
hard	B-Device
drive	I-Device
can	O
be	O
sorted	O
using	O
1GB	O
of	O
main	O
memory	O
before	O
a	O
third	O
pass	O
becomes	O
advantageous	O
,	O
and	O
many	O
times	O
that	O
much	O
data	O
can	O
be	O
sorted	O
before	O
a	O
fourth	O
pass	O
becomes	O
useful	O
.	O
</s>
<s>
Doubling	O
memory	O
dedicated	O
to	O
sorting	B-Algorithm
halves	O
the	O
number	O
of	O
chunks	O
and	O
the	O
number	O
of	O
reads	O
per	O
chunk	O
,	O
reducing	O
the	O
number	O
of	O
seeks	O
required	O
by	O
about	O
three-quarters	O
.	O
</s>
<s>
The	O
ratio	O
of	O
RAM	B-Architecture
to	O
disk	B-Device
storage	I-Device
on	O
servers	O
often	O
makes	O
it	O
convenient	O
to	O
do	O
huge	O
sorts	O
on	O
a	O
cluster	O
of	O
machines	O
rather	O
than	O
on	O
one	O
machine	O
with	O
multiple	O
passes	O
.	O
</s>
<s>
Media	O
with	O
high	O
random-read	O
performance	O
like	O
solid-state	B-Device
drives	I-Device
(	O
SSDs	B-Device
)	O
also	O
increase	O
the	O
amount	O
that	O
can	O
be	O
sorted	O
before	O
additional	O
passes	O
improve	O
performance	O
.	O
</s>
<s>
External	O
distribution	O
sort	O
is	O
analogous	O
to	O
quicksort	B-Algorithm
.	O
</s>
<s>
When	O
the	O
subarrays	O
are	O
less	O
than	O
the	O
block	O
size	O
,	O
sorting	B-Algorithm
can	O
be	O
done	O
quickly	O
because	O
all	O
reads	O
and	O
writes	O
are	O
done	O
in	O
the	O
cache	B-General_Concept
,	O
and	O
in	O
the	O
external	B-Application
memory	I-Application
model	I-Application
requires	O
operations	O
.	O
</s>
<s>
However	O
,	O
finding	O
exactly	O
pivots	O
would	O
not	O
be	O
fast	O
enough	O
to	O
make	O
the	O
external	O
distribution	O
sort	O
asymptotically	B-General_Concept
optimal	I-General_Concept
.	O
</s>
<s>
To	O
find	O
these	O
pivots	O
,	O
the	O
algorithm	O
splits	O
the	O
input	O
elements	O
into	O
chunks	O
,	O
and	O
takes	O
every	O
elements	O
,	O
and	O
recursively	O
uses	O
the	O
median	B-Algorithm
of	I-Algorithm
medians	I-Algorithm
algorithm	I-Algorithm
to	O
find	O
pivots	O
.	O
</s>
<s>
The	O
,	O
created	O
by	O
computer	O
scientist	O
Jim	O
Gray	O
,	O
compares	O
external	B-Algorithm
sorting	I-Algorithm
algorithms	O
implemented	O
using	O
finely	O
tuned	O
hardware	O
and	O
software	O
.	O
</s>
<s>
Multiple	O
disk	B-Device
drives	I-Device
can	O
be	O
used	O
in	O
parallel	O
in	O
order	O
to	O
improve	O
sequential	O
read	O
and	O
write	O
speed	O
.	O
</s>
<s>
This	O
can	O
be	O
a	O
very	O
cost-efficient	O
improvement	O
:	O
a	O
Sort	O
Benchmark	O
winner	O
in	O
the	O
cost-centric	O
Penny	O
Sort	O
category	O
uses	O
six	O
hard	B-Device
drives	I-Device
in	O
an	O
otherwise	O
midrange	O
machine	O
.	O
</s>
<s>
Sorting	B-Algorithm
software	O
can	O
use	O
multiple	B-Operating_System
threads	I-Operating_System
,	O
to	O
speed	O
up	O
the	O
process	O
on	O
modern	O
multicore	O
computers	O
.	O
</s>
<s>
Software	O
can	O
use	O
asynchronous	B-Architecture
I/O	I-Architecture
so	O
that	O
one	O
run	O
of	O
data	O
can	O
be	O
sorted	O
or	O
merged	O
while	O
other	O
runs	O
are	O
being	O
read	O
from	O
or	O
written	O
to	O
disk	B-Device
.	O
</s>
<s>
Using	O
more	O
RAM	B-Architecture
for	O
sorting	B-Algorithm
can	O
reduce	O
the	O
number	O
of	O
disk	B-Device
seeks	O
and	O
avoid	O
the	O
need	O
for	O
more	O
passes	O
.	O
</s>
<s>
Fast	O
external	B-Application
memory	I-Application
like	O
solid-state	B-Device
drives	I-Device
can	O
speed	O
sorts	O
,	O
either	O
if	O
the	O
data	O
is	O
small	O
enough	O
to	O
fit	O
entirely	O
on	O
SSDs	B-Device
or	O
,	O
more	O
rarely	O
,	O
to	O
accelerate	O
sorting	B-Algorithm
SSD-sized	O
chunks	O
in	O
a	O
three-pass	O
sort	O
.	O
</s>
<s>
Many	O
other	O
factors	O
can	O
affect	O
hardware	O
's	O
maximum	O
sorting	B-Algorithm
speed	O
:	O
CPU	O
speed	O
and	O
number	O
of	O
cores	O
,	O
RAM	B-Architecture
access	O
latency	O
,	O
input/output	O
bandwidth	O
,	O
disk	B-Device
read/write	O
speed	O
,	O
disk	B-Device
seek	O
time	O
,	O
and	O
others	O
.	O
</s>
<s>
"	O
Balancing	O
"	O
the	O
hardware	O
to	O
minimize	O
bottlenecks	O
is	O
an	O
important	O
part	O
of	O
designing	O
an	O
efficient	O
sorting	B-Algorithm
system	O
.	O
</s>
<s>
Some	O
Sort	O
Benchmark	O
entrants	O
use	O
a	O
variation	O
on	O
radix	B-Algorithm
sort	I-Algorithm
for	O
the	O
first	O
phase	O
of	O
sorting	B-Algorithm
:	O
they	O
separate	O
data	O
into	O
one	O
of	O
many	O
"	O
bins	O
"	O
based	O
on	O
the	O
beginning	O
of	O
its	O
value	O
.	O
</s>
<s>
Because	O
the	O
Sort	O
Benchmark	O
sorts	O
long	O
(	O
100-byte	O
)	O
records	O
using	O
short	O
(	O
10-byte	O
)	O
keys	O
,	O
sorting	B-Algorithm
software	O
sometimes	O
rearranges	O
the	O
keys	O
separately	O
from	O
the	O
values	O
to	O
reduce	O
memory	O
I/O	O
volume	O
.	O
</s>
