title: Most Significant Digit Radix Sort in Java
body: <p>I wrote the code for most significant radix sort but I keep getting an error of IndexBoundsException on the line: arr[i] = arr1.get(i);. And now I don't know what's wrong because the logic seems right to me.</p>
  
  <pre><code>  public static int[] msdRadixSort(int[] arr) {
      if (arr == null) {
          throw new IllegalArgumentException("The array is null!");
      }
      //search through to find msd
      int temp = arr[0];
      for (int i = 0; i &lt; arr.length; i++) {
  
          if (Math.abs(temp) &lt;= Math.abs(arr[i])) {
              temp = arr[i];
          }
      }
      //find the length of the msd
      int k = 1;
      while (!(temp / 10 == 0)) {
          temp = temp / 10;
          k++;
      }
      ArrayList&lt;Integer&gt; arr1 = new ArrayList&lt;Integer&gt;(arr.length);
      for (int i = 0; i &lt; arr.length; i++) {
          arr1.add(arr[i]);
      }
      msdRadixSort(arr1, k);
      System.out.println(arr1);
      for (int i = 0; i &lt; arr.length; i++) {
          arr[i] = arr1.get(i);
      }
      return arr;
  }
  </code></pre>