Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; either version 2
- of the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- public class Radix
- {
- LinkedList work, b, c, sorted;
- int largestLen = 0; // stores the amount of digits of the largest number (after converted to binary)
- public Radix() // constructor
- {
- work = new LinkedList(); // (also the working set)
- b = new LinkedList(); // 0s
- c = new LinkedList(); // 1s
- sorted = new LinkedList();
- }
- public void insert(int numero) // insert into work list and also converting to binary integer
- {
- int binary;
- String binaryString;
- binaryString = Integer.toBinaryString(numero);
- if (binaryString.length() > largestLen)
- largestLen = binaryString.length();
- binary = Integer.parseInt(binaryString);
- work.queue(binary);
- }
- public void displayRadix()
- {
- work.displayList();
- }
- /* sorts the binary forms of the number using the radix sort */
- public void sort()
- {
- System.out.println(largestLen);
- int mod = 10;
- int div = 1;
- Node currentNode; // temporay reference
- for (int x = 0; x <= largestLen; x++) { // this loop should stop after each digit is sorted
- while (!work.isEmpty()) {
- currentNode = work.dequeue();
- if ( ((currentNode.data % mod) / div) == 0 )
- b.queue(currentNode.data);
- else
- c.queue(currentNode.data);
- }
- while (!b.isEmpty()){
- currentNode = b.dequeue();
- work.queue(currentNode.data);
- }
- while (!c.isEmpty()) {
- currentNode = c.dequeue();
- work.queue(currentNode.data);
- }
- mod = mod * 10;
- div = div * 10;
- } // end for loop
- } // end of sort method
- } // end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement