Sort the Queue using Recursion

Given a queue and the task is to sort it using recursion without using any loop. We can only use the following functions of queue:

empty(q): Tests whether the queue is empty or not.
push(q): Adds a new element to the queue.
pop(q): Removes front element from the queue.
size(q): Returns the number of elements in a queue.
front(q): Returns the value of the front element without removing it.


Input: queue = {10, 7, 16, 9, 20, 5}
Output: 5 7 9 10 16 20

Input: queue = {0, -2, -1, 2, 3, 1}
Output: -2 -1 0 1 2 3

Approach: The idea of the solution is to hold all values in the function call stack until the queue becomes empty. When the queue becomes empty, insert all held items one by one in sorted order. Here sorted order is important.
How to manage sorted order?
Whenever you get the item from the function call stack, then first calculate the size of the queue and compare it with the elements of queue. Here, two cases arises:

  1. If the item (returned by function call stack) is greater then the front element of the queue then dequeue front element and enqueue this element into the same queue by decreasing the size.
  2. If the item is less than the front element from the queue then enqueue the element in the queue and dequeue the remaining element from the queue and enqueue by decreasing size repeat the case 1 and 2 unless size becomes zero. Take care of one thing, if size became zero and your element remains greater then all elements of the queue then push your element into the queue.

Below is the implementation of the above approach:





// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to push elment in last by
// popping from front until size becomes 0
void FrontToLast(queue<int>& q, int qsize)
    // Base condition
    if (qsize <= 0)
    // pop front element and push
    // this last in a queue
    // Recursive call for pushing element
    FrontToLast(q, qsize - 1);
// Function to push an element in the queue
// while maintaining the sorted order
void pushInQueue(queue<int>& q, int temp, int qsize)
    // Base condition
    if (q.empty() || qsize == 0) {
    // If current element is less than
    // the element at the front
    else if (temp <= q.front()) {
        // Call stack with front of queue
        // Recursive call for inserting a front
        // element of the queue to the last
        FrontToLast(q, qsize);
    else {
        // Push front element into
        // last in a queue
        // Recursive call for pushing
        // element in a queue
        pushInQueue(q, temp, qsize - 1);
// Function to sort the given
// queue using recursion
void sortQueue(queue<int>& q)
    // Return if queue is empty
    if (q.empty())
    // Get the front element which will
    // be stored in this variable
    // throughout the recursion stack
    int temp = q.front();
    // Remove the front element
    // Recursive call
    // Push the current element into the queue
    // according to the sorting order
    pushInQueue(q, temp, q.size());
// Driver code
int main()
    // Push elements to the queue
    queue<int> qu;
    // Sort the queue
    // Print the elements of the
    // queue after sorting
    while (!qu.empty()) {
        cout << qu.front() << " ";



5 7 9 10 16 20

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.