Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by Roman Zhuravlev on 22.06.2017.
- */
- public class RadixSorter
- {
- private int digit;
- private int size;
- private int counter;
- private int maxDiss;
- private int []currentArray;
- private int[] tempArr;
- private int [][]radixArray;
- public RadixSorter(){
- counter = 0;
- digit = 1;
- size = 0;
- tempArr = new int[10];
- for(int i=0; i<10; i++)
- tempArr[i]=0;
- }
- public void load(int[] currentArray) {
- this.currentArray = currentArray;
- this.size = currentArray.length;
- radixArray = new int[10][size];
- maxDiss = maxDis();
- }
- public void setSize(int size) {
- this.size = size;
- }
- public int getDigit() {
- return this.digit;
- }
- public int[] getWorkingArray(){
- return this.currentArray;
- }
- public int[] getRadixArray(int row) {
- int []tempArray = new int[this.size];
- for(int i = 0; i<this.size; i++){
- tempArray[i] = radixArray[row][i];
- }
- return tempArray;
- }
- public boolean doStep(){
- if(this.counter < this.currentArray.length) {
- int a = disNumber(this.currentArray[counter]);
- this.radixArray[a][this.tempArr[a]] = this.currentArray[counter];
- tempArr[a]++;
- this.counter++;
- return true;
- } else {
- this.digit++;
- int tempPos = 0;
- for(int i=0; i<10; i++)
- {
- for(int j=0; j<tempArr[i]; j++)
- {
- this.currentArray[tempPos] = this.radixArray[i][j];
- tempPos++;
- }
- }
- clean();
- return false;
- }
- }
- public void sort(){
- for(int i = 0; i< maxDiss; i++) {
- while (doStep()) {
- }
- }
- }
- private void clean(){
- counter = 0;
- for(int i=0; i<10; i++)
- tempArr[i]=0;
- }
- private void clear(){
- }
- private int disNumber(int number)
- {
- int digitTemp = this.digit;
- while(digitTemp>1)
- {
- number/=10;
- digitTemp--;
- }
- return number%10;
- }
- public int maxDis(){
- int temp = this.currentArray[0];
- for(int i = 1; i < size; i++){
- if(this.currentArray[i]>temp) temp = this.currentArray[i];
- }
- int count =0;
- while(temp > 0){
- temp/=10;
- count++;
- }
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement