<s>
In	O
computer	B-General_Concept
programming	I-General_Concept
,	O
the	O
Schwartzian	B-Algorithm
transform	I-Algorithm
is	O
a	O
technique	O
used	O
to	O
improve	O
the	O
efficiency	O
of	O
sorting	B-Algorithm
a	O
list	O
of	O
items	O
.	O
</s>
<s>
This	O
idiom	O
is	O
appropriate	O
for	O
comparison-based	B-Algorithm
sorting	I-Algorithm
when	O
the	O
ordering	O
is	O
actually	O
based	O
on	O
the	O
ordering	O
of	O
a	O
certain	O
property	O
(	O
the	O
key	O
)	O
of	O
the	O
elements	O
,	O
where	O
computing	O
that	O
property	O
is	O
an	O
intensive	O
operation	O
that	O
should	O
be	O
performed	O
a	O
minimal	O
number	O
of	O
times	O
.	O
</s>
<s>
The	O
Schwartzian	B-Algorithm
transform	I-Algorithm
is	O
notable	O
in	O
that	O
it	O
does	O
not	O
use	O
named	O
temporary	O
arrays	O
.	O
</s>
<s>
The	O
Schwartzian	B-Algorithm
transform	I-Algorithm
is	O
a	O
version	O
of	O
a	O
Lisp	B-Language
idiom	O
known	O
as	O
decorate-sort-undecorate	B-Algorithm
,	O
which	O
avoids	O
recomputing	O
the	O
sort	O
keys	O
by	O
temporarily	O
associating	O
them	O
with	O
the	O
input	O
items	O
.	O
</s>
<s>
The	O
idiom	O
is	O
named	O
after	O
Randal	O
L	O
.	O
Schwartz	O
,	O
who	O
first	O
demonstrated	O
it	O
in	O
Perl	B-Language
shortly	O
after	O
the	O
release	O
of	O
Perl	B-Language
5	O
in	O
1994	O
.	O
</s>
<s>
The	O
term	O
"	O
Schwartzian	B-Algorithm
transform	I-Algorithm
"	O
applied	O
solely	O
to	O
Perl	B-Language
programming	I-Language
for	O
a	O
number	O
of	O
years	O
,	O
but	O
it	O
has	O
later	O
been	O
adopted	O
by	O
some	O
users	O
of	O
other	O
languages	O
,	O
such	O
as	O
Python	B-Language
,	O
to	O
refer	O
to	O
similar	O
idioms	O
in	O
those	O
languages	O
.	O
</s>
<s>
However	O
,	O
the	O
algorithm	O
was	O
already	O
in	O
use	O
in	O
other	O
languages	O
(	O
under	O
no	O
specific	O
name	O
)	O
before	O
it	O
was	O
popularized	O
among	O
the	O
Perl	B-Language
community	O
in	O
the	O
form	O
of	O
that	O
particular	O
idiom	O
by	O
Schwartz	O
.	O
</s>
<s>
The	O
term	O
"	O
Schwartzian	B-Algorithm
transform	I-Algorithm
"	O
indicates	O
a	O
specific	O
idiom	O
,	O
and	O
not	O
the	O
algorithm	O
in	O
general	O
.	O
</s>
<s>
To	O
make	O
it	O
a	O
true	O
Schwartzian	B-Algorithm
transform	I-Algorithm
,	O
it	O
would	O
be	O
done	O
in	O
Perl	B-Language
like	O
this	O
:	O
</s>
<s>
The	O
general	O
form	O
of	O
the	O
Schwartzian	B-Algorithm
transform	I-Algorithm
is	O
:	O
</s>
<s>
finally	O
,	O
another	O
map	O
operation	O
unwraps	O
the	O
values	O
(	O
from	O
the	O
anonymous	O
array	O
)	O
used	O
for	O
the	O
sorting	B-Algorithm
,	O
producing	O
the	O
items	O
of	O
the	O
original	O
list	O
in	O
the	O
sorted	O
order	O
(	O
sorted	O
list	O
of	O
[	O
item	O
,	O
value ]	O
⇒	O
sorted	O
list	O
of	O
item	O
)	O
.	O
</s>
<s>
The	O
use	O
of	O
anonymous	O
arrays	O
ensures	O
that	O
memory	O
will	O
be	O
reclaimed	O
by	O
the	O
Perl	B-Language
garbage	O
collector	O
immediately	O
after	O
the	O
sorting	B-Algorithm
is	O
done	O
.	O
</s>
<s>
Without	O
the	O
Schwartzian	B-Algorithm
transform	I-Algorithm
,	O
the	O
sorting	B-Algorithm
in	O
the	O
example	O
above	O
would	O
be	O
written	O
in	O
Perl	B-Language
like	O
this	O
:	O
</s>
<s>
An	O
optimal	O
comparison	B-Algorithm
sort	I-Algorithm
performs	O
O(n log n )	O
comparisons	O
(	O
where	O
n	O
is	O
the	O
length	O
of	O
the	O
list	O
)	O
,	O
with	O
2	O
calls	O
to	O
every	O
comparison	O
,	O
resulting	O
in	O
O(n log n )	O
calls	O
to	O
.	O
</s>
<s>
In	O
comparison	O
,	O
using	O
the	O
Schwartzian	B-Algorithm
transform	I-Algorithm
,	O
we	O
only	O
make	O
1	O
call	O
to	O
per	O
element	O
,	O
at	O
the	O
beginning	O
stage	O
,	O
for	O
a	O
total	O
of	O
n	O
calls	O
to	O
.	O
</s>
<s>
However	O
,	O
if	O
the	O
function	O
is	O
relatively	O
simple	O
,	O
then	O
the	O
extra	O
overhead	O
of	O
the	O
Schwartzian	B-Algorithm
transform	I-Algorithm
may	O
be	O
unwarranted	O
.	O
</s>
<s>
For	O
example	O
,	O
to	O
sort	O
a	O
list	O
of	O
files	O
by	O
their	O
modification	B-Application
times	I-Application
,	O
a	O
naive	O
approach	O
might	O
be	O
as	O
follows	O
:	O
</s>
<s>
Unless	O
the	O
modification	B-Application
times	I-Application
are	O
for	O
each	O
file	O
,	O
this	O
method	O
requires	O
re-computing	O
them	O
every	O
time	O
a	O
file	O
is	O
compared	O
in	O
the	O
sort	O
.	O
</s>
<s>
Using	O
the	O
Schwartzian	B-Algorithm
transform	I-Algorithm
,	O
the	O
modification	O
time	O
is	O
calculated	O
only	O
once	O
per	O
file	O
.	O
</s>
<s>
A	O
Schwartzian	B-Algorithm
transform	I-Algorithm
involves	O
the	O
functional	O
idiom	O
described	O
above	O
,	O
which	O
does	O
not	O
use	O
temporary	O
arrays	O
.	O
</s>
<s>
The	O
same	O
algorithm	O
can	O
be	O
written	O
procedurally	O
to	O
better	O
illustrate	O
how	O
it	O
works	O
,	O
but	O
this	O
requires	O
using	O
temporary	O
arrays	O
,	O
and	O
is	O
not	O
a	O
Schwartzian	B-Algorithm
transform	I-Algorithm
.	O
</s>
<s>
The	O
first	O
known	O
online	O
appearance	O
of	O
the	O
Schwartzian	B-Algorithm
transform	I-Algorithm
is	O
a	O
December	O
16	O
,	O
1994	O
to	O
a	O
thread	O
in	O
comp.unix.shell	O
Usenet	B-Application
newsgroup	I-Application
,	O
crossposted	O
to	O
comp.lang.perl	O
.	O
</s>
<s>
Schwartz	O
noted	O
in	O
the	O
post	O
that	O
he	O
was	O
"Speak[ing]	O
with	O
a	O
lisp	B-Language
in	O
Perl	B-Language
"	O
,	O
a	O
reference	O
to	O
the	O
idiom	O
's	O
Lisp	B-Language
origins	O
.	O
</s>
<s>
The	O
term	O
"	O
Schwartzian	B-Algorithm
transform	I-Algorithm
"	O
itself	O
was	O
coined	O
by	O
Tom	O
Christiansen	O
in	O
a	O
follow-up	O
reply	O
.	O
</s>
<s>
Some	O
other	O
languages	O
provide	O
a	O
convenient	O
interface	O
to	O
the	O
same	O
optimization	O
as	O
the	O
Schwartzian	B-Algorithm
transform	I-Algorithm
:	O
</s>
<s>
In	O
Python	B-Language
2.4	O
and	O
above	O
,	O
both	O
the	O
function	O
and	O
the	O
in-place	O
method	O
take	O
a	O
parameter	O
that	O
allows	O
the	O
user	O
to	O
provide	O
a	O
"	O
key	O
function	O
"	O
(	O
like	O
in	O
the	O
examples	O
above	O
)	O
.	O
</s>
<s>
In	O
Python	B-Language
3	O
and	O
above	O
,	O
use	O
of	O
the	O
key	O
function	O
is	O
the	O
only	O
way	O
to	O
specify	O
a	O
custom	O
sort	O
order	O
(	O
the	O
previously	O
supported	O
parameter	O
that	O
allowed	O
the	O
user	O
to	O
provide	O
a	O
"	O
comparison	O
function	O
"	O
was	O
removed	O
)	O
.	O
</s>
<s>
Before	O
Python	B-Language
2.4	O
,	O
developers	O
would	O
use	O
the	O
lisp-originated	O
decorate	O
–	O
sort	O
–	O
undecorate	O
(	O
DSU	O
)	O
idiom	O
,	O
usually	O
by	O
wrapping	O
the	O
objects	O
in	O
a	O
(	O
sortkey	O
,	O
object	O
)	O
tuple	O
.	O
</s>
<s>
In	O
Ruby	B-Language
1.8.6	O
and	O
above	O
,	O
the	O
abstract	O
class	O
(	O
which	O
includes	O
s	O
)	O
contains	O
a	O
method	O
,	O
which	O
allows	O
specifying	O
the	O
"	O
key	O
function	O
"	O
(	O
like	O
in	O
the	O
examples	O
above	O
)	O
as	O
a	O
code	O
block	O
.	O
</s>
<s>
In	O
D	B-Application
2	O
and	O
above	O
,	O
the	O
function	O
is	O
available	O
.	O
</s>
<s>
It	O
might	O
require	O
less	O
temporary	O
data	O
and	O
be	O
faster	O
than	O
the	O
Perl	B-Language
idiom	O
or	O
the	O
decorate	O
–	O
sort	O
–	O
undecorate	O
idiom	O
present	O
in	O
Python	B-Language
and	O
Lisp	B-Language
.	O
</s>
<s>
This	O
is	O
because	O
sorting	B-Algorithm
is	O
done	O
in-place	O
,	O
and	O
only	O
minimal	O
extra	O
data	O
(	O
one	O
array	O
of	O
transformed	O
elements	O
)	O
is	O
created	O
.	O
</s>
<s>
Racket	B-Operating_System
's	I-Operating_System
core	O
sort	O
function	O
accepts	O
a	O
#	O
:key	O
keyword	O
argument	O
with	O
a	O
function	O
that	O
extracts	O
a	O
key	O
,	O
and	O
an	O
additional	O
#	O
:cache-keys	O
?	O
</s>
<s>
requests	O
that	O
the	O
resulting	O
values	O
are	O
cached	O
during	O
sorting	B-Algorithm
.	O
</s>
<s>
In	O
PHP	B-Application
5.3	O
and	O
above	O
the	O
transform	O
can	O
be	O
implemented	O
by	O
use	O
of	O
,	O
e.g.	O
</s>
<s>
to	O
work	O
around	O
the	O
limitations	O
of	O
the	O
unstable	O
sort	O
algorithms	O
in	O
PHP	B-Application
.	O
</s>
<s>
In	O
Elixir	B-Language
,	O
the	O
and	O
methods	O
allow	O
users	O
to	O
perform	O
a	O
Schwartzian	B-Algorithm
transform	I-Algorithm
for	O
any	O
module	O
that	O
implements	O
the	O
protocol	O
.	O
</s>
<s>
In	O
Raku	B-Application
,	O
one	O
needs	O
to	O
supply	O
a	O
comparator	O
lambda	O
that	O
only	O
takes	O
1	O
argument	O
to	O
perform	O
a	O
Schwartzian	B-Algorithm
transform	I-Algorithm
under	O
the	O
hood	O
:	O
would	O
sort	O
on	O
the	O
string	O
representation	O
using	O
a	O
Schwartzian	B-Algorithm
transform	I-Algorithm
,	O
would	O
do	O
the	O
same	O
converting	O
the	O
elements	O
to	O
compare	O
just	O
before	O
each	O
comparison	O
.	O
</s>
<s>
In	O
Rust	B-Application
,	O
somewhat	O
confusingly	O
,	O
the	O
method	O
does	O
*	O
not*	O
perform	O
a	O
Schwartzian	B-Algorithm
transform	I-Algorithm
as	O
it	O
will	O
not	O
allocate	O
additional	O
storage	O
for	O
the	O
key	O
,	O
it	O
will	O
call	O
the	O
key	O
function	O
for	O
each	O
value	O
for	O
each	O
comparison	O
.	O
</s>
