Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. /*This program is free software; you can redistribute it and/or
  2. modify it under the terms of the GNU General Public License
  3. as published by the Free Software Foundation; either version 2
  4. of the License, or (at your option) any later version.
  5.  
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9. GNU General Public License for more details.
  10.  
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  14. */
  15. public class Radix
  16. {
  17.     LinkedList work, b, c, sorted;
  18.     int largestLen = 0; // stores the amount of digits of the largest number (after converted to binary)
  19.     public Radix() // constructor
  20.     {
  21.         work = new LinkedList(); // (also the working set)
  22.         b = new LinkedList(); // 0s
  23.         c = new LinkedList(); // 1s
  24.         sorted = new LinkedList();
  25.     }
  26.  
  27.     public void insert(int numero) // insert into work list and also converting to binary integer
  28.     {
  29.         int binary;
  30.         String binaryString;
  31.         binaryString = Integer.toBinaryString(numero);
  32.         if (binaryString.length() > largestLen)
  33.             largestLen = binaryString.length();
  34.         binary = Integer.parseInt(binaryString);
  35.         work.queue(binary);
  36.     }
  37.  
  38.     public void displayRadix()
  39.     {
  40.         work.displayList();
  41.     }
  42.  
  43. /* sorts the binary forms of the number using the radix sort */
  44.     public void sort()
  45.     {
  46.         System.out.println(largestLen);
  47.         int mod  = 10;
  48.         int div = 1;
  49.         Node currentNode; // temporay reference
  50.         for (int x = 0; x <= largestLen; x++) { // this loop should stop after each digit is sorted
  51.             while (!work.isEmpty()) {
  52.                 currentNode = work.dequeue();
  53.                 if ( ((currentNode.data % mod) / div) == 0 )
  54.                     b.queue(currentNode.data);
  55.                 else
  56.                     c.queue(currentNode.data);
  57.             }
  58.  
  59.             while (!b.isEmpty()){
  60.                 currentNode = b.dequeue();
  61.                 work.queue(currentNode.data);
  62.             }
  63.  
  64.             while (!c.isEmpty()) {
  65.                 currentNode = c.dequeue();
  66.                 work.queue(currentNode.data);
  67.             }
  68.             mod = mod * 10;
  69.             div = div * 10;
  70.         } // end for loop
  71.  
  72.     } // end of sort method
  73.  
  74. } // end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement