Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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).</p> <p>So first of all, the homework was about "Taxies" (you know how homework is...) and a TaxiQueue, in Java.</p> <p>The solution was:</p> <pre><code>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() &lt; 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); } </code></pre> <p>I hope that other students may find it useful some day!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload