11package de .moritzf .sorting .logic .sorting ;
22
33import java .util .ArrayList ;
4+ import java .util .List ;
45
56/**
67 * @author Moritz Floeter
@@ -36,7 +37,7 @@ public class RadixSort extends SortingAlgorithm {
3637 /**
3738 * The input array.
3839 */
39- private int [] inputArray ;
40+ private List < String > inputArray ;
4041
4142 /**
4243 * Instantiates a new instance of radixsort.
@@ -45,31 +46,46 @@ public class RadixSort extends SortingAlgorithm {
4546 */
4647 public RadixSort (int [] input ) {
4748 RadixStep firstStep = new RadixStep ();
48- ArrayList <String > inputAsStrings = new ArrayList <String >();
4949 int max = 0 ;
50- this .inputArray = input ;
50+ this .inputArray = convertToLengthAdjustedStrings ( input ) ;
5151
52+
53+ sortIntoBoxes (this .inputArray , firstStep );
54+
55+ protocol .add (firstStep );
56+ }
57+
58+ /**
59+ * Converts a list of numbers to a list of Strings and prepends 0s in
60+ * order to guarantee the same length for every number-String.
61+ *
62+ * @param input the array
63+ * @return the converted array
64+ */
65+ private static List <String > convertToLengthAdjustedStrings (int [] input ){
66+
67+ List <String > inputAsStrings = new ArrayList <>();
68+ int max = 0 ;
5269 // find the maximum length of one input element (eg. 43 has the length 2, 154 has the length 3 etc.)
5370 for (int i = 0 ; i < input .length ; i ++) {
5471 if (max < ("" + input [i ]).length ()) {
5572 max = ("" + input [i ]).length ();
5673 }
5774 }
5875
59- /*
76+ /*
6077 * all elements shorter than the longest element get filled with zeroes
61- * so that they have the same length when represented as String
62- */
78+ * so that they have the same length when represented as String
79+ */
6380 for (int i = 0 ; i < input .length ; i ++) {
6481 String input2add = "" + input [i ];
6582 while (input2add .length () < max ) {
6683 input2add = "0" + input2add ;
6784 }
6885 inputAsStrings .add (input2add );
6986 }
70- sortIntoBoxes (inputAsStrings , firstStep );
7187
72- protocol . add ( firstStep ) ;
88+ return inputAsStrings ;
7389 }
7490
7591
@@ -90,7 +106,7 @@ private int getNumberLength() {
90106 * @param collectedStrings the collected strings
91107 * @param step the step
92108 */
93- private static void sortIntoBoxes (ArrayList <String > collectedStrings , RadixStep step ) {
109+ private static void sortIntoBoxes (List <String > collectedStrings , RadixStep step ) {
94110 // as memory marks the position counting from the right end of the
95111 // string, the position gets set accordingly
96112 int position = collectedStrings .get (0 ).length () - 1 - step .getMemory ();
@@ -146,7 +162,7 @@ private String collect2LaTeX(int stepNumber) {
146162 retString += ", " ;
147163 }
148164 //add linebreaks after 10 elements.
149- if ((i + 1 ) % 10 == 0 && this .inputArray .length > i + 1 ) {
165+ if ((i + 1 ) % 10 == 0 && this .inputArray .size () > i + 1 ) {
150166 retString += "\\ \\ " ;
151167 }
152168 }
@@ -162,13 +178,14 @@ private String collect2LaTeX(int stepNumber) {
162178 */
163179 private String originalArray2LaTeX () {
164180 String originalArray = "" ;
165- for (int i = 0 ; i < this .inputArray .length ; i ++) {
166- originalArray = originalArray + this .inputArray [ i ] ;
167- if (this .inputArray .length > i + 1 ) {
181+ for (int i = 0 ; i < this .inputArray .size () ; i ++) {
182+ originalArray = originalArray + this .inputArray . get ( i ) ;
183+ if (this .inputArray .size () > i + 1 ) {
168184 originalArray += ", " ;
169185 }
186+
170187 //add linebreaks after 10 elements.
171- if ((i + 1 ) % 10 == 0 && this .inputArray .length > i + 1 ) {
188+ if ((i + 1 ) % 10 == 0 && this .inputArray .size () > i + 1 ) {
172189 originalArray += "\\ \\ " ;
173190 }
174191 }
@@ -335,6 +352,4 @@ public int getStepLimit() {
335352 return STEP_LIMIT ;
336353 }
337354
338-
339-
340355}
0 commit comments