Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | None | 0 0
  1. /**
  2.  * Created by Roman Zhuravlev on 22.06.2017.
  3.  */
  4. public class RadixSorter
  5. {
  6.     private int digit;
  7.     private int size;
  8.     private int counter;
  9.     private int maxDiss;
  10.     private int []currentArray;
  11.     private int[] tempArr;
  12.     private int [][]radixArray;
  13.  
  14.     public RadixSorter(){
  15.         counter = 0;
  16.         digit = 1;
  17.         size = 0;
  18.  
  19.         tempArr = new int[10];
  20.  
  21.         for(int i=0; i<10; i++)
  22.             tempArr[i]=0;
  23.     }
  24.  
  25.     public void load(int[] currentArray) {
  26.         this.currentArray = currentArray;
  27.         this.size = currentArray.length;
  28.         radixArray = new int[10][size];
  29.         maxDiss = maxDis();
  30.     }
  31.  
  32.     public void setSize(int size) {
  33.         this.size = size;
  34.     }
  35.  
  36.     public int getDigit() {
  37.         return this.digit;
  38.     }
  39.  
  40.     public int[] getWorkingArray(){
  41.         return this.currentArray;
  42.     }
  43.  
  44.     public int[] getRadixArray(int row) {
  45.         int []tempArray = new int[this.size];
  46.         for(int i = 0; i<this.size; i++){
  47.             tempArray[i] = radixArray[row][i];
  48.         }
  49.         return tempArray;
  50.     }
  51.  
  52.     public boolean doStep(){
  53.         if(this.counter < this.currentArray.length) {
  54.             int a = disNumber(this.currentArray[counter]);
  55.             this.radixArray[a][this.tempArr[a]] = this.currentArray[counter];
  56.             tempArr[a]++;
  57.             this.counter++;
  58.             return true;
  59.         } else {
  60.             this.digit++;
  61.  
  62.             int tempPos = 0;
  63.  
  64.             for(int i=0; i<10; i++)
  65.             {
  66.                 for(int j=0; j<tempArr[i]; j++)
  67.                 {
  68.                     this.currentArray[tempPos] = this.radixArray[i][j];
  69.                     tempPos++;
  70.  
  71.                 }
  72.             }
  73.  
  74.             clean();
  75.             return false;
  76.         }
  77.     }
  78.  
  79.     public void sort(){
  80.         for(int i = 0; i< maxDiss; i++) {
  81.             while (doStep()) {
  82.  
  83.             }
  84.         }
  85.     }
  86.  
  87.     private void clean(){
  88.         counter = 0;
  89.         for(int i=0; i<10; i++)
  90.             tempArr[i]=0;
  91.     }
  92.     private void clear(){
  93.  
  94.     }
  95.  
  96.     private int disNumber(int number)
  97.     {
  98.         int digitTemp = this.digit;
  99.         while(digitTemp>1)
  100.         {
  101.             number/=10;
  102.             digitTemp--;
  103.         }
  104.         return number%10;
  105.     }
  106.  
  107.     public int maxDis(){
  108.         int temp = this.currentArray[0];
  109.         for(int i = 1; i < size; i++){
  110.             if(this.currentArray[i]>temp) temp = this.currentArray[i];
  111.         }
  112.         int count =0;
  113.         while(temp > 0){
  114.             temp/=10;
  115.             count++;
  116.         }
  117.         return count;
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement