home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / PERM_IDX.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  1KB  |  42 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. ** Determine the permutation index for a given permutation list.
  5. ** Written by Thad Smith III, Boulder, CO  8/31/91
  6. ** Hereby contributed to the Public Domain.
  7. **
  8. ** The following function computes the ordinal of the given permutation,
  9. ** which is index of the permutation in sorting order:
  10. **  1, 2, ..., n-1, n   is index 0
  11. **  1, 2, ..., n, n-1   is index 1
  12. **  ...
  13. **  n, n-1, ..., 2, 1   is index n! -1
  14. **
  15. ** The actual values of the elements are immaterial, only the relative
  16. ** ordering of the values is used.
  17. **
  18. ** pit[] is the array of elements of length size.
  19. ** The return value is the permutation index.
  20. */
  21.  
  22. #include "snipmath.h"
  23.  
  24. int perm_index (char pit[], int size)
  25. {
  26.       int i;
  27.       register int j, ball;
  28.       int index = 0;
  29.  
  30.       for (i = 1; i < size; i++)
  31.       {
  32.             ball = pit[i-1];
  33.             for (j = i; j < size; j++)
  34.             {
  35.                   if (ball > pit[j])
  36.                         index ++;
  37.             }
  38.             index *= size - i;
  39.       }
  40.       return index;
  41. }
  42.