Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7416477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:30:19+00:00 2026-05-29T07:30:19+00:00

The question is this: You need to sort a queue in a new member

  • 0

The question is this:

You need to sort a queue in a new member method void sort() you add to the queue.

The rules are:

  • You can not use any loops. Only recursion is allowed.
  • You can create as many private methods as you wish.
  • You may create and use two auxiliary queues of the same kind (call it Queue – has a default constructor).
  • You don’t know anything about the internal structure of the queue.
  • The only methods given to you are: bool empty(), Data peek(), Data dequeue(), void enqueue(Data d)
  • dequeue() returns null if the list is empty, enqueue() ignores null inputs.
  • The sorting order needs to be ascending (front of queue should have the smallest value).
  • The comparable value is in integer inside the Data structure, called x.

I know it can be solved. I have yet to find an answer for this. Any ideas would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:30:21+00:00Added an answer on May 29, 2026 at 7:30 am

    First of all, thanks for the answers. They pointed me to the right solution. I voted them up to give credit, but since I managed to solve this question, I’ll accept my own answer (at least until a better one comes).

    So first of all, the homework was about “Taxies” (you know how homework is…) and a TaxiQueue, in Java.

    The solution was:

    public void sort() {
        sort(this);
    }
    
    private void sort(TaxiQueue toSort) {
        // Prepare split parts for later merging   
        TaxiQueue m1 = new TaxiQueue(), m2 = new TaxiQueue();
    
        // Return if there's only 1 element in the queue
        // since it's essentially "sorted".
        if(singleItem(toSort))
            return;
    
        // Split toSort into 2 parts
        split(toSort, m1, m2);
        // Sort each part recursively (by splitting and merging)
        sort(m1);
        sort(m2);
        // Merge each part into 1 sorted queue
        merge(toSort,  m1, m2);
    }
    
    private boolean singleItem(TaxiQueue tq) {
        Taxi temp = tq.dequeue();
        boolean retVal = tq.empty();
        tq.enqueue(temp);
        return retVal;
    }
    
    private void merge(TaxiQueue toSort, TaxiQueue m1, TaxiQueue m2) {
        // Notice that m1 and m2 are already sorted, and now we need
        // to merge them.
        // In the case that one of them is empty and the other one isn't,
        // simply append all remaining "higher" values into toSort.
        if(m1.empty()) {
            appendQueue(m2, toSort);
            return;
        }
        else if (m2.empty()) {
            appendQueue(m1, toSort);
            return;
        }
        // The critical comparison part...
        if(m1.peek().getId() < m2.peek().getId())
            toSort.enqueue(m1.dequeue());
        else
            toSort.enqueue(m2.dequeue());
        // Continue merging elements into toSort.
        merge(toSort, m1, m2);
    }
    
    // Split toSort into m1 and m2 as equally as possible
    private void split(TaxiQueue toSort, TaxiQueue m1, TaxiQueue m2) {
        if(toSort.empty())
            return;
        m1.enqueue(toSort.dequeue());
        split(toSort,  m2, m1);
    }
    
    // Enqueue everything in src to dest.
    private void appendQueue(TaxiQueue src, TaxiQueue dest) {
        if (src.empty())
            return;
        dest.enqueue(src.dequeue());
        appendQueue(src, dest);
    }
    

    I hope that other students may find it useful some day!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So this question is not so much technical but more sort of conceptual. I
This question comes close to what I need, but my scenario is slightly different.
This question is geared towards a group of newly hired developers that need to
I really need an answer to this question. I am working on a project
Following up on this question, I need to get exactly n lines at random
This question is regarding getopt function in php. I need to pass two parameter
EDIT: This question was initially too general, I think. So What I really need
I edited this question after i found a solution... i need to understand why
According to this question I succeeded to create upload image, but now I need
For reasons that are irrelevant to this question I'll need to run several SQLite

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.