<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
double-ended	B-Application
priority	I-Application
queue	I-Application
(	O
DEPQ	B-Application
)	O
or	O
double-ended	B-Application
heap	I-Application
is	O
a	O
data	B-General_Concept
structure	I-General_Concept
similar	O
to	O
a	O
priority	B-Application
queue	I-Application
or	O
heap	B-Application
,	O
but	O
allows	O
for	O
efficient	O
removal	O
of	O
both	O
the	O
maximum	O
and	O
minimum	O
,	O
according	O
to	O
some	O
ordering	O
on	O
the	O
keys	O
(	O
items	O
)	O
stored	O
in	O
the	O
structure	O
.	O
</s>
<s>
Every	O
element	O
in	O
a	O
DEPQ	B-Application
has	O
a	O
priority	O
or	O
value	O
.	O
</s>
<s>
In	O
a	O
DEPQ	B-Application
,	O
it	O
is	O
possible	O
to	O
remove	O
the	O
elements	O
in	O
both	O
ascending	O
as	O
well	O
as	O
descending	O
order	O
.	O
</s>
<s>
A	O
double-ended	B-Application
priority	I-Application
queue	I-Application
features	O
the	O
following	O
operations	O
:	O
</s>
<s>
isEmpty( )	O
Checks	O
if	O
DEPQ	B-Application
is	O
empty	O
and	O
returns	O
true	O
if	O
empty	O
.	O
</s>
<s>
size( )	O
Returns	O
the	O
total	O
number	O
of	O
elements	O
present	O
in	O
the	O
DEPQ	B-Application
.	O
</s>
<s>
put(x )	O
Inserts	O
the	O
element	O
x	O
in	O
the	O
DEPQ	B-Application
.	O
</s>
<s>
Also	O
,	O
the	O
priority	O
of	O
any	O
element	O
can	O
be	O
changed	O
once	O
it	O
has	O
been	O
inserted	O
in	O
the	O
DEPQ	B-Application
.	O
</s>
<s>
Double-ended	B-Application
priority	I-Application
queues	I-Application
can	O
be	O
built	O
from	O
balanced	B-Data_Structure
binary	I-Data_Structure
search	I-Data_Structure
trees	I-Data_Structure
(	O
where	O
the	O
minimum	O
and	O
maximum	O
elements	O
are	O
the	O
leftmost	O
and	O
rightmost	O
leaves	O
,	O
respectively	O
)	O
,	O
or	O
using	O
specialized	O
data	B-General_Concept
structures	I-General_Concept
like	O
min-max	B-Application
heap	I-Application
and	O
pairing	B-Application
heap	I-Application
.	O
</s>
<s>
Generic	O
methods	O
of	O
arriving	O
at	O
double-ended	B-Application
priority	I-Application
queues	I-Application
from	O
normal	O
priority	B-Application
queues	I-Application
are	O
:	O
</s>
<s>
In	O
this	O
method	O
two	O
different	O
priority	B-Application
queues	I-Application
for	O
min	O
and	O
max	O
are	O
maintained	O
.	O
</s>
<s>
Here	O
,	O
the	O
minimum	O
and	O
maximum	O
elements	O
are	O
values	O
contained	O
in	O
the	O
root	O
nodes	O
of	O
min	B-Application
heap	I-Application
and	O
max	B-Application
heap	I-Application
respectively	O
.	O
</s>
<s>
Removing	O
the	O
min	O
element	O
:	O
Perform	O
removemin( )	O
on	O
the	O
min	B-Application
heap	I-Application
and	O
remove(node value )	O
on	O
the	O
max	B-Application
heap	I-Application
,	O
where	O
node	O
value	O
is	O
the	O
value	O
in	O
the	O
corresponding	O
node	O
in	O
the	O
max	B-Application
heap	I-Application
.	O
</s>
<s>
Removing	O
the	O
max	O
element	O
:	O
Perform	O
removemax( )	O
on	O
the	O
max	B-Application
heap	I-Application
and	O
remove(node value )	O
on	O
the	O
min	B-Application
heap	I-Application
,	O
where	O
node	O
value	O
is	O
the	O
value	O
in	O
the	O
corresponding	O
node	O
in	O
the	O
min	B-Application
heap	I-Application
.	O
</s>
<s>
If	O
the	O
number	O
of	O
elements	O
in	O
the	O
DEPQ	B-Application
is	O
odd	O
,	O
one	O
of	O
the	O
elements	O
is	O
retained	O
in	O
a	O
buffer	O
.	O
</s>
<s>
If	O
the	O
number	O
of	O
elements	O
in	O
the	O
DEPQ	B-Application
is	O
odd	O
,	O
one	O
of	O
the	O
elements	O
is	O
retained	O
in	O
a	O
buffer	O
.	O
</s>
<s>
Apart	O
from	O
the	O
above-mentioned	O
correspondence	O
methods	O
,	O
DEPQ	B-Application
's	O
can	O
be	O
obtained	O
efficiently	O
using	O
interval	O
heaps	B-Application
.	O
</s>
<s>
An	O
interval	O
heap	B-Application
is	O
like	O
an	O
embedded	O
min-max	B-Application
heap	I-Application
in	O
which	O
each	O
node	O
contains	O
two	O
elements	O
.	O
</s>
<s>
Elements	O
on	O
the	O
left	O
hand	O
side	O
define	O
a	O
min	B-Application
heap	I-Application
.	O
</s>
<s>
Elements	O
on	O
the	O
right	O
hand	O
side	O
define	O
a	O
max	B-Application
heap	I-Application
.	O
</s>
<s>
Depending	O
on	O
the	O
number	O
of	O
elements	O
already	O
present	O
in	O
the	O
interval	O
heap	B-Application
,	O
following	O
cases	O
are	O
possible	O
:	O
</s>
<s>
Odd	O
number	O
of	O
elements	O
:	O
If	O
the	O
number	O
of	O
elements	O
in	O
the	O
interval	O
heap	B-Application
is	O
odd	O
,	O
the	O
new	O
element	O
is	O
firstly	O
inserted	O
in	O
the	O
last	O
node	O
.	O
</s>
<s>
Then	O
,	O
it	O
is	O
successively	O
compared	O
with	O
the	O
previous	O
node	O
elements	O
and	O
tested	O
to	O
satisfy	O
the	O
criteria	O
essential	O
for	O
an	O
interval	O
heap	B-Application
as	O
stated	O
above	O
.	O
</s>
<s>
If	O
the	O
element	O
falls	O
to	O
the	O
left	O
of	O
the	O
parent	O
interval	O
,	O
it	O
is	O
considered	O
to	O
be	O
in	O
the	O
min	B-Application
heap	I-Application
and	O
if	O
the	O
element	O
falls	O
to	O
the	O
right	O
of	O
the	O
parent	O
interval	O
,	O
it	O
is	O
considered	O
in	O
the	O
max	B-Application
heap	I-Application
.	O
</s>
<s>
Further	O
,	O
it	O
is	O
compared	O
successively	O
and	O
moved	O
from	O
the	O
last	O
node	O
to	O
the	O
root	O
until	O
all	O
the	O
conditions	O
for	O
interval	O
heap	B-Application
are	O
satisfied	O
.	O
</s>
<s>
Min	O
element	O
:	O
In	O
an	O
interval	O
heap	B-Application
,	O
the	O
minimum	O
element	O
is	O
the	O
element	O
on	O
the	O
left	O
hand	O
side	O
of	O
the	O
root	O
node	O
.	O
</s>
<s>
This	O
element	O
is	O
then	O
compared	O
successively	O
with	O
all	O
the	O
left	O
hand	O
elements	O
of	O
the	O
descending	O
nodes	O
and	O
the	O
process	O
stops	O
when	O
all	O
the	O
conditions	O
for	O
an	O
interval	O
heap	B-Application
are	O
satisfied	O
.	O
</s>
<s>
Max	O
element	O
:	O
In	O
an	O
interval	O
heap	B-Application
,	O
the	O
maximum	O
element	O
is	O
the	O
element	O
on	O
the	O
right	O
hand	O
side	O
of	O
the	O
root	O
node	O
.	O
</s>
<s>
Thus	O
,	O
with	O
interval	O
heaps	B-Application
,	O
both	O
the	O
minimum	O
and	O
maximum	O
elements	O
can	O
be	O
removed	O
efficiently	O
traversing	O
from	O
root	O
to	O
leaf	O
.	O
</s>
<s>
Thus	O
,	O
a	O
DEPQ	B-Application
can	O
be	O
obtained	O
from	O
an	O
interval	O
heap	B-Application
where	O
the	O
elements	O
of	O
the	O
interval	O
heap	B-Application
are	O
the	O
priorities	O
of	O
elements	O
in	O
the	O
DEPQ	B-Application
.	O
</s>
<s>
When	O
DEPQ	B-Application
's	O
are	O
implemented	O
using	O
heaps	B-Application
or	O
pairing	B-Application
heaps	I-Application
consisting	O
of	O
n	O
elements	O
,	O
the	O
time	O
complexities	O
for	O
the	O
various	O
functions	O
are	O
formulated	O
in	O
the	O
table	O
below	O
.	O
</s>
<s>
For	O
pairing	B-Application
heaps	I-Application
,	O
it	O
is	O
an	O
amortized	B-General_Concept
complexity	I-General_Concept
.	O
</s>
<s>
One	O
example	O
application	O
of	O
the	O
double-ended	B-Application
priority	I-Application
queue	I-Application
is	O
external	B-Algorithm
sorting	I-Algorithm
.	O
</s>
<s>
In	O
an	O
external	B-Algorithm
sort	I-Algorithm
,	O
there	O
are	O
more	O
elements	O
than	O
can	O
be	O
held	O
in	O
the	O
computer	O
's	O
memory	O
.	O
</s>
<s>
The	O
external	O
quick	B-Algorithm
sort	I-Algorithm
is	O
implemented	O
using	O
the	O
DEPQ	B-Application
as	O
follows	O
:	O
</s>
<s>
Read	O
in	O
as	O
many	O
elements	O
as	O
will	O
fit	O
into	O
an	O
internal	O
DEPQ	B-Application
.	O
</s>
<s>
The	O
elements	O
in	O
the	O
DEPQ	B-Application
will	O
eventually	O
be	O
the	O
middle	O
group	O
(	O
pivot	O
)	O
of	O
elements	O
.	O
</s>
<s>
If	O
the	O
next	O
element	O
is	O
≤	O
the	O
smallest	O
element	O
in	O
the	O
DEPQ	B-Application
,	O
output	O
this	O
next	O
element	O
as	O
part	O
of	O
the	O
left	O
group	O
.	O
</s>
<s>
If	O
the	O
next	O
element	O
is	O
≥	O
the	O
largest	O
element	O
in	O
the	O
DEPQ	B-Application
,	O
output	O
this	O
next	O
element	O
as	O
part	O
of	O
the	O
right	O
group	O
.	O
</s>
<s>
Otherwise	O
,	O
remove	O
either	O
the	O
max	O
or	O
min	O
element	O
from	O
the	O
DEPQ	B-Application
(	O
the	O
choice	O
may	O
be	O
made	O
randomly	O
or	O
alternately	O
)	O
;	O
if	O
the	O
max	O
element	O
is	O
removed	O
,	O
output	O
it	O
as	O
part	O
of	O
the	O
right	O
group	O
;	O
otherwise	O
,	O
output	O
the	O
removed	O
element	O
as	O
part	O
of	O
the	O
left	O
group	O
;	O
insert	O
the	O
newly	O
input	O
element	O
into	O
the	O
DEPQ	B-Application
.	O
</s>
<s>
Output	O
the	O
elements	O
in	O
the	O
DEPQ	B-Application
,	O
in	O
sorted	O
order	O
,	O
as	O
the	O
middle	O
group	O
.	O
</s>
