<s>
Graham	B-Algorithm
's	I-Algorithm
scan	I-Algorithm
is	O
a	O
method	O
of	O
finding	O
the	O
convex	O
hull	O
of	O
a	O
finite	O
set	O
of	O
points	O
in	O
the	O
plane	O
with	O
time	O
complexity	O
O(n log n )	O
.	O
</s>
<s>
It	O
uses	O
a	O
stack	B-Application
to	O
detect	O
and	O
remove	O
concavities	O
in	O
the	O
boundary	O
efficiently	O
.	O
</s>
<s>
Any	O
general-purpose	O
sorting	B-Algorithm
algorithm	I-Algorithm
is	O
appropriate	O
for	O
this	O
,	O
for	O
example	O
heapsort	B-Application
(	O
which	O
is	O
O(n log n )	O
)	O
.	O
</s>
<s>
Sorting	B-Algorithm
in	O
order	O
of	O
angle	O
does	O
not	O
require	O
computing	O
the	O
angle	O
.	O
</s>
<s>
If	O
numeric	B-Algorithm
precision	O
is	O
at	O
stake	O
,	O
the	O
comparison	O
function	O
used	O
by	O
the	O
sorting	B-Algorithm
algorithm	I-Algorithm
can	O
use	O
the	O
sign	O
of	O
the	O
cross	O
product	O
to	O
determine	O
relative	O
angles	O
.	O
</s>
<s>
This	O
process	O
will	O
eventually	O
return	O
to	O
the	O
point	O
at	O
which	O
it	O
started	O
,	O
at	O
which	O
point	O
the	O
algorithm	O
is	O
completed	O
and	O
the	O
stack	B-Application
now	O
contains	O
the	O
points	O
on	O
the	O
convex	O
hull	O
in	O
counterclockwise	O
order	O
.	O
</s>
<s>
Sorting	B-Algorithm
the	O
points	O
has	O
time	O
complexity	O
O(n log n )	O
.	O
</s>
<s>
(	O
In	O
real	O
applications	O
,	O
if	O
the	O
coordinates	O
are	O
arbitrary	O
real	O
numbers	O
,	O
the	O
function	O
requires	O
exact	O
comparison	O
of	O
floating-point	B-Algorithm
numbers	I-Algorithm
,	O
and	O
one	O
has	O
to	O
beware	O
of	O
numeric	B-Algorithm
singularities	O
for	O
"	O
nearly	O
"	O
collinear	O
points	O
.	O
)	O
</s>
<s>
Then	O
let	O
the	O
result	O
be	O
stored	O
in	O
the	O
stack	B-Application
.	O
</s>
<s>
while	O
count	O
stack	B-Application
>	O
1	O
and	O
ccw(next_to_top(stack )	O
,	O
top(stack )	O
,	O
point	O
)	O
<=	O
0	O
:	O
</s>
<s>
Now	O
the	O
stack	B-Application
contains	O
the	O
convex	O
hull	O
,	O
where	O
the	O
points	O
are	O
oriented	O
counter-clockwise	O
and	O
P0	O
is	O
the	O
first	O
point	O
.	O
</s>
<s>
Here	O
,	O
next_to_top( )	O
is	O
a	O
function	O
for	O
returning	O
the	O
item	O
one	O
entry	O
below	O
the	O
top	O
of	O
stack	B-Application
,	O
without	O
changing	O
the	O
stack	B-Application
,	O
and	O
similarly	O
,	O
top( )	O
for	O
returning	O
the	O
topmost	O
element	O
.	O
</s>
