title: Sort integers in any base (radix)
body: <p>I am supposed to write an algorithm in C that takes an array of integers in any base (radix) and use Radix Sort to put them in ascending order. My algorithm can not impose any limit for the length of the array, the number of digits and the base. When I say integers I do not mean I will always receive an array of <code>int</code>s, it can be an array of <code>long int</code>s, <code>char</code>s, <code>char</code> pointers, etc.</p>
  <p>Seen that, I though the best way to deal with that imposing as little as possible limitations to my algorithm was to just deal with the vector by accessing it's positions and type casting the value there to <code>long unsigned int</code> so the limitation this approach has is that the user must use a digit &quot;weight&quot; that complies with the encoding option the code was compiled with, i.e., in the ASCII encoding <code>x</code> in decimal base is 120 and <code>a</code> is 97, so if the given base uses the digits <code>x</code> and <code>a</code>, <code>x</code> must be greater than <code>a</code>.</p>
  <p>Here is my algorithm:</p>
  <pre><code>/*!
   *  @function   radixSort
   *  @abstract   Sort a vector using Radix Sort.
   *  @discussion This function take a vector of positive
   *              integers in any base (radix) and sorts
   *              it in ascending order using the Radix
   *              Sort algorithm.
   *  @param      v       the vector with elements to sort
   *  @param      n       the length of the vector v
   *  @param      getMax  the function to get the maximum element of v
   */
  void radixSort(void *v[], long unsigned int n, long unsigned int (*getMax)(void*[], long unsigned int)) {
      long unsigned int i;
      void* semiSorted[n];
      long unsigned int significantDigit = 1;
      long unsigned int max = (*getMax)(v, n);
      while (max / significantDigit &gt; 0){
          long unsigned int intermediate[10] = {0};
          for(i=0; i&lt;n; i++)
              intermediate[(long unsigned int)v[i]/significantDigit % 10]++;
          for(i=1; i&lt;10; i++)
              intermediate[i] += intermediate[i-1];
          for(i=n; i&gt;0; i--)
              semiSorted[--intermediate[(long unsigned int)v[i-1]/significantDigit % 10]] = v[i-1];
          for(i=0; i&lt;n; i++)
              v[i] = semiSorted[i];
          significantDigit *= 10;
      }
  }
  </code></pre>
  <p>This algorithm works with all types I tested, but a string array.
  If I have something like:</p>
  <pre><code>int main() {
      char *b36v[7] = {&quot;014&quot;, &quot;000&quot;, &quot;8f6&quot;, &quot;080&quot;, &quot;056&quot;, &quot;00a&quot;, &quot;080&quot;};
      printf(&quot;b36v unsorted: &quot;); for(i=0; i&lt;7; i++) printf(&quot;%s &quot;, b36v[i]); printf(&quot;\n&quot;);
      radixSort(b36v, 7, getMaxFunc);
      printf(&quot;b36v sorted  : &quot;); for(i=0; i&lt;7; i++) printf(&quot;%s &quot;, b36v[i]); printf(&quot;\n&quot;);
      return 0;
  }
  </code></pre>
  <p>The result output will be:</p>
  <blockquote>
  <p>b36v unsorted: 014 000 8f6 080 056 00a 080</p>
  <p>b36v sorted  : 014 000 8f6 080 080 056 00a</p>
  </blockquote>
  <p>So it doesn't work as expected, I expected the sorted vector to be:</p>
  <blockquote>
  <p>000 00a 014 056 080 080 8f6</p>
  </blockquote>
  <p>I can only see that equal entries are grouped together as expected. Changing the values and observing the outputs I think probably the algorithm is taking the memory addresses and sorting them because at a given position of the original vector there is no actual value, but an array of <code>char</code>'s.</p>
  <p>I just want to know if there is someway I can handle these cases of 2D (or even 3D, 4D, ...) arrays writing an universal code which handle from <code>int</code>/<code>char</code> arrays to arrays of arrays of arrays... of <code>long unsigned int</code>s and without asking for too much parameters. I can't figure out how to manage this situation in C.</p>