title: Radix Algorithm Understanding in Java
body: <p>Below is the code for radix Algorithm from www.sanfoundry.com.  I don't understand the third for loop in while loop: why int i must be counted inversely and how this loop works to resign ordered elements to b[]. And one more question, what if we regard number as based 2 following the similar code? And are there any possibility to modify this code for mixture of positive and negative numbers?</p>
  
  <pre><code>public static void radixSort(int[] input){
    final int N = input.length;
    int exp = 1;  int[] b = new int[N];
    int max = input[0];
    for(int i = 1; i &lt; N; i++){
      if(max &lt;= input[i]) max = input[i];
    }
    while(max / exp &gt; 0){
      int[] bucket = new int[10];
      for(int i = 0; i &lt; N; i++)  bucket[(input[i] / exp ) % 10]++;
      for(int i = 1; i &lt; 10; i++) bucket[i] += bucket[i - 1];
      for(int i = N - 1; i &gt;= 0; i--) b[--bucket[(input[i] / exp) % 10]] = input[i];
      for(int i = 0; i &lt; N; i++) input[i] = b[i];
      exp *= 10; 
    }
  }
  </code></pre>