<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
selection	B-Algorithm
sort	I-Algorithm
is	O
an	O
in-place	B-Algorithm
comparison	B-Algorithm
sorting	I-Algorithm
algorithm	O
.	O
</s>
<s>
It	O
has	O
an	O
O(n2 )	O
time	O
complexity	O
,	O
which	O
makes	O
it	O
inefficient	O
on	O
large	O
lists	O
,	O
and	O
generally	O
performs	O
worse	O
than	O
the	O
similar	O
insertion	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
Selection	B-Algorithm
sort	I-Algorithm
is	O
noted	O
for	O
its	O
simplicity	O
and	O
has	O
performance	O
advantages	O
over	O
more	O
complicated	O
algorithms	O
in	O
certain	O
situations	O
,	O
particularly	O
where	O
auxiliary	O
memory	O
is	O
limited	O
.	O
</s>
<s>
The	O
algorithm	O
proceeds	O
by	O
finding	O
the	O
smallest	O
(	O
or	O
largest	O
,	O
depending	O
on	O
sorting	B-Algorithm
order	O
)	O
element	O
in	O
the	O
unsorted	O
sublist	O
,	O
exchanging	O
(	O
swapping	O
)	O
it	O
with	O
the	O
leftmost	O
unsorted	O
element	O
(	O
putting	O
it	O
in	O
sorted	O
order	O
)	O
,	O
and	O
moving	O
the	O
sublist	O
boundaries	O
one	O
element	O
to	O
the	O
right	O
.	O
</s>
<s>
The	O
time	O
efficiency	O
of	O
selection	B-Algorithm
sort	I-Algorithm
is	O
quadratic	O
,	O
so	O
there	O
are	O
a	O
number	O
of	O
sorting	B-Algorithm
techniques	O
which	O
have	O
better	O
time	O
complexity	O
than	O
selection	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
Here	O
is	O
an	O
example	O
of	O
this	O
sort	B-Algorithm
algorithm	I-Algorithm
sorting	B-Algorithm
five	O
elements	O
:	O
</s>
<s>
Selection	B-Algorithm
sort	I-Algorithm
can	O
also	O
be	O
used	O
on	O
list	O
structures	O
that	O
make	O
add	O
and	O
remove	O
efficient	O
,	O
such	O
as	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
.	O
</s>
<s>
Below	O
is	O
an	O
implementation	O
in	O
C	B-Language
.	O
</s>
<s>
Selection	B-Algorithm
sort	I-Algorithm
is	O
not	O
difficult	O
to	O
analyze	O
compared	O
to	O
other	O
sorting	B-Algorithm
algorithms	I-Algorithm
,	O
since	O
none	O
of	O
the	O
loops	O
depend	O
on	O
the	O
data	O
in	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
Among	O
quadratic	O
sorting	B-Algorithm
algorithms	I-Algorithm
(	O
sorting	B-Algorithm
algorithms	I-Algorithm
with	O
a	O
simple	O
average-case	O
of	O
Θ(n2 )	O
)	O
,	O
selection	B-Algorithm
sort	I-Algorithm
almost	O
always	O
outperforms	O
bubble	B-Algorithm
sort	I-Algorithm
and	O
gnome	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
Insertion	B-Algorithm
sort	I-Algorithm
is	O
very	O
similar	O
in	O
that	O
after	O
the	O
kth	O
iteration	O
,	O
the	O
first	O
elements	O
in	O
the	O
array	B-Data_Structure
are	O
in	O
sorted	O
order	O
.	O
</s>
<s>
Insertion	B-Algorithm
sort	I-Algorithm
's	O
advantage	O
is	O
that	O
it	O
only	O
scans	O
as	O
many	O
elements	O
as	O
it	O
needs	O
in	O
order	O
to	O
place	O
the	O
st	O
element	O
,	O
while	O
selection	B-Algorithm
sort	I-Algorithm
must	O
scan	O
all	O
remaining	O
elements	O
to	O
find	O
the	O
st	O
element	O
.	O
</s>
<s>
Simple	O
calculation	O
shows	O
that	O
insertion	B-Algorithm
sort	I-Algorithm
will	O
therefore	O
usually	O
perform	O
about	O
half	O
as	O
many	O
comparisons	O
as	O
selection	B-Algorithm
sort	I-Algorithm
,	O
although	O
it	O
can	O
perform	O
just	O
as	O
many	O
or	O
far	O
fewer	O
depending	O
on	O
the	O
order	O
the	O
array	B-Data_Structure
was	O
in	O
prior	O
to	O
sorting	B-Algorithm
.	O
</s>
<s>
It	O
can	O
be	O
seen	O
as	O
an	O
advantage	O
for	O
some	O
real-time	B-General_Concept
applications	I-General_Concept
that	O
selection	B-Algorithm
sort	I-Algorithm
will	O
perform	O
identically	O
regardless	O
of	O
the	O
order	O
of	O
the	O
array	B-Data_Structure
,	O
while	O
insertion	B-Algorithm
sort	I-Algorithm
's	O
running	O
time	O
can	O
vary	O
considerably	O
.	O
</s>
<s>
However	O
,	O
this	O
is	O
more	O
often	O
an	O
advantage	O
for	O
insertion	B-Algorithm
sort	I-Algorithm
in	O
that	O
it	O
runs	O
much	O
more	O
efficiently	O
if	O
the	O
array	B-Data_Structure
is	O
already	O
sorted	O
or	O
"	O
close	O
to	O
sorted.	O
"	O
</s>
<s>
While	O
selection	B-Algorithm
sort	I-Algorithm
is	O
preferable	O
to	O
insertion	B-Algorithm
sort	I-Algorithm
in	O
terms	O
of	O
number	O
of	O
writes	O
(	O
swaps	O
versus	O
up	O
to	O
swaps	O
,	O
with	O
each	O
swap	O
being	O
two	O
writes	O
)	O
,	O
this	O
is	O
roughly	O
twice	O
the	O
theoretical	O
minimum	O
achieved	O
by	O
cycle	B-Language
sort	I-Language
,	O
which	O
performs	O
at	O
most	O
n	O
writes	O
.	O
</s>
<s>
This	O
can	O
be	O
important	O
if	O
writes	O
are	O
significantly	O
more	O
expensive	O
than	O
reads	O
,	O
such	O
as	O
with	O
EEPROM	B-General_Concept
or	O
Flash	B-Device
memory	I-Device
,	O
where	O
every	O
write	O
lessens	O
the	O
lifespan	O
of	O
the	O
memory	O
.	O
</s>
<s>
Selection	B-Algorithm
sort	I-Algorithm
can	O
be	O
implemented	O
without	O
unpredictable	O
branches	O
for	O
the	O
benefit	O
of	O
CPU	O
branch	B-General_Concept
predictors	I-General_Concept
,	O
by	O
finding	O
the	O
location	O
of	O
the	O
minimum	O
with	O
branch-free	O
code	O
and	O
then	O
performing	O
the	O
swap	O
unconditionally	O
.	O
</s>
<s>
Finally	O
,	O
selection	B-Algorithm
sort	I-Algorithm
is	O
greatly	O
outperformed	O
on	O
larger	O
arrays	O
by	O
divide-and-conquer	B-Algorithm
algorithms	I-Algorithm
such	O
as	O
mergesort	B-Algorithm
.	O
</s>
<s>
However	O
,	O
insertion	B-Algorithm
sort	I-Algorithm
or	O
selection	B-Algorithm
sort	I-Algorithm
are	O
both	O
typically	O
faster	O
for	O
small	O
arrays	O
(	O
i.e.	O
</s>
<s>
A	O
useful	O
optimization	O
in	O
practice	O
for	O
the	O
recursive	O
algorithms	O
is	O
to	O
switch	O
to	O
insertion	B-Algorithm
sort	I-Algorithm
or	O
selection	B-Algorithm
sort	I-Algorithm
for	O
"	O
small	O
enough	O
"	O
sublists	O
.	O
</s>
<s>
Heapsort	B-Application
greatly	O
improves	O
the	O
basic	O
algorithm	O
by	O
using	O
an	O
implicit	B-Data_Structure
heap	B-Application
data	I-Application
structure	I-Application
to	O
speed	O
up	O
finding	O
and	O
removing	O
the	O
lowest	O
datum	O
.	O
</s>
<s>
If	O
implemented	O
correctly	O
,	O
the	O
heap	B-Application
will	O
allow	O
finding	O
the	O
next	O
lowest	O
element	O
in	O
time	O
instead	O
of	O
for	O
the	O
inner	O
loop	O
in	O
normal	O
selection	B-Algorithm
sort	I-Algorithm
,	O
reducing	O
the	O
total	O
running	O
time	O
to	O
.	O
</s>
<s>
A	O
bidirectional	O
variant	O
of	O
selection	B-Algorithm
sort	I-Algorithm
(	O
called	O
double	O
selection	B-Algorithm
sort	I-Algorithm
or	O
sometimes	O
cocktail	B-Algorithm
sort	I-Algorithm
due	O
to	O
its	O
similarity	O
to	O
cocktail	B-Algorithm
shaker	I-Algorithm
sort	I-Algorithm
)	O
finds	O
both	O
the	O
minimum	O
and	O
maximum	O
values	O
in	O
the	O
list	O
in	O
every	O
pass	O
.	O
</s>
<s>
This	O
requires	O
three	O
comparisons	O
per	O
two	O
items	O
(	O
a	O
pair	O
of	O
elements	O
is	O
compared	O
,	O
then	O
the	O
greater	O
is	O
compared	O
to	O
the	O
maximum	O
and	O
the	O
lesser	O
is	O
compared	O
to	O
the	O
minimum	O
)	O
rather	O
than	O
regular	O
selection	B-Algorithm
sort	I-Algorithm
's	O
one	O
comparison	B-Algorithm
per	O
item	O
,	O
but	O
requires	O
only	O
half	O
as	O
many	O
passes	O
,	O
a	O
net	O
25%	O
savings	O
.	O
</s>
<s>
Selection	B-Algorithm
sort	I-Algorithm
can	O
be	O
implemented	O
as	O
a	O
stable	O
sort	O
if	O
,	O
rather	O
than	O
swapping	O
in	O
step	O
2	O
,	O
the	O
minimum	O
value	O
is	O
inserted	O
into	O
the	O
first	O
position	O
and	O
the	O
intervening	O
values	O
shifted	O
up	O
.	O
</s>
<s>
However	O
,	O
this	O
modification	O
either	O
requires	O
a	O
data	B-General_Concept
structure	I-General_Concept
that	O
supports	O
efficient	O
insertions	O
or	O
deletions	O
,	O
such	O
as	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
,	O
or	O
it	O
leads	O
to	O
performing	O
writes	O
.	O
</s>
<s>
In	O
the	O
bingo	B-Algorithm
sort	I-Algorithm
variant	O
,	O
items	O
are	O
sorted	O
by	O
repeatedly	O
looking	O
through	O
the	O
remaining	O
items	O
to	O
find	O
the	O
greatest	O
value	O
and	O
moving	O
all	O
items	O
with	O
that	O
value	O
to	O
their	O
final	O
location	O
.	O
</s>
<s>
Like	O
counting	B-Algorithm
sort	I-Algorithm
,	O
this	O
is	O
an	O
efficient	O
variant	O
if	O
there	O
are	O
many	O
duplicate	O
values	O
:	O
selection	B-Algorithm
sort	I-Algorithm
does	O
one	O
pass	O
through	O
the	O
remaining	O
items	O
for	O
each	O
item	O
moved	O
,	O
while	O
Bingo	B-Algorithm
sort	I-Algorithm
does	O
one	O
pass	O
for	O
each	O
value	O
.	O
</s>
<s>
After	O
an	O
initial	O
pass	O
to	O
find	O
the	O
greatest	O
value	O
,	O
subsequent	O
passes	O
move	O
every	O
item	O
with	O
that	O
value	O
to	O
its	O
final	O
location	O
while	O
finding	O
the	O
next	O
value	O
as	O
in	O
the	O
following	O
pseudocode	B-Language
(	O
arrays	O
are	O
zero-based	O
and	O
the	O
for-loop	O
includes	O
both	O
the	O
top	O
and	O
bottom	O
limits	O
,	O
as	O
in	O
Pascal	B-Application
)	O
:	O
</s>
<s>
Thus	O
,	O
if	O
on	O
average	O
there	O
are	O
more	O
than	O
two	O
items	O
with	O
the	O
same	O
value	O
,	O
bingo	B-Algorithm
sort	I-Algorithm
can	O
be	O
expected	O
to	O
be	O
faster	O
because	O
it	O
executes	O
the	O
inner	O
loop	O
fewer	O
times	O
than	O
selection	B-Algorithm
sort	I-Algorithm
.	O
</s>
