Here's a method I think might work :-
The algorithm uses Recursion to sort the queue.
Assume we have a function copy_from that can copy from one queue over to another queue as follows :-
void copy_from (ArrayQueue&Q_from,ArrayQueue& Q_to){
while(!Q_from.empty()){
int t= Q_from.front();
Q_to.enqueue(t);
Q_from.dequeue();
}
}
The main sort function is as shown :-
void sort(ArrayQueue &Q, int element, ArrayQueue&Q2){
if(Q.size()==1){
if(Q.front()>element){
int front = Q.front();
Q.dequeue();
Q.enqueue(element);
Q.enqueue(front);
}
else{
Q.enqueue(element);
}
}
else{
int front = Q.front();
Q.dequeue();
sort(Q,front); // sort the remaining queue.
int sorted_front = Q.front(); //get front element of sorted queue
if(element<sorted_front){
Q2.enqueue(element);
copy_from(Q,Q2);
copy_from(Q2,Q);
}
else{
// dequeue all elements from the queue which are lesser than element and put it into new queue.
// Then enqueue the new element
// Then enqueue whatevers bigger than element into the queue.
Q2.enqueue(sorted_front);
Q.dequeue();
while(!Q.empty()&&Q.front()<element){
Q2.enqueue(Q.front());
Q.dequeue();
}
Q2.enqueue(element);
copy_from(Q,Q2);
copy_from(Q2,Q);
}
}
}
When we initially call the sort function, we can call it as follows :-
Queue Q, Q2;
int front = Q.front();
Q.dequeue();
sort(Q,front,Q2);
If we had input as 6->4->9>2->1, the result would be 9->6->4->2->1.