Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can implement a Selection sort fairly easily with a Stack and Queue:</p> <p>If the data is originally on the stack, put it all on the Queue, and do the following:</p> <pre><code>int size = list.length; // list is the int[] sent as a method parameter. while (size &gt; 0) { // until we've placed all elements in their sorted positions int min = Integer.MAX_VALUE; // make sure the minimum is bigger than all the data for (int i = 0; i &lt; size; i++) { // check all elements in the queue int temp = queue.dequeue(); // mark the current element if (temp &lt; min) min = temp; // if it's a possible minimum record it. queue.enqueue(temp); // place it back in the queue so we don't lose data. } for (int i = 0; i &lt; size; i++) { // go through and remove the minimum element int temp = queue.dequeue(); // mark the currently removed element if (temp == min) break; // if it's the minimum, stop we've removed it. queue.enqueue(temp); // otherwise put it back on. } stack.push(min); // put the minimum value on the stack (in sorted order) size--; // we have one less element to consider, so decrement the size. } </code></pre> <p>After the sort is over the elements will be sorted on the Stack, removing them all will return data in order Greatest to Least (which can easily be reversed).</p> <p>No this is not very efficient, i'm assuming it's a homework assignment. Realisticaly if you want to sort data you should use Arrays.sort or Collections.sort which implement a merge sort for objects and a quicksort for primitives. These will be far faster (O(NlogN) vs O(N*N)). Realize however you'll need to have data in an array or a list to do this, not a stack or queue.</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. 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.
    3. VO
      singulars
      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