home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mntdoc01.zoo / mintdoc / cat3 / qsort.3 < prev    next >
Encoding:
Text File  |  1993-03-03  |  2.4 KB  |  133 lines

  1.  
  2.  
  3.  
  4. QSORT(3)            MINTLIB LIBRARY FUNCTIONS            QSORT(3)
  5.  
  6.  
  7. N✓NA✓AM✓ME✓E
  8.        qsort - quicker sort
  9.  
  10. S✓SY✓YN✓NO✓OP✓PS✓SI✓IS✓S
  11.        #include <stdlib.h>
  12.  
  13.        qsort(void *base, size_t total_elems, size_t elem_size,
  14.              int (*compare)(const void *one, const void *two));
  15.  
  16. D✓DE✓ES✓SC✓CR✓RI✓IP✓PT✓TI✓IO✓ON✓N
  17.        qsort  is an implementation of the quicker-sort algorithm.
  18.        It sorts a table of data in place.
  19.          - base points to the element at the base of
  20.            the table.
  21.          - total_elems is the number of elements in
  22.            the table.
  23.          - elem_size is the size, in bytes, of each element
  24.            in the table.
  25.          - compare is the name of the comparison function,
  26.            which is called with two arguments that point to
  27.            the elements being compared. As the function must
  28.            return an integer less than, equal to, or greater
  29.            than zero, so must the first argument to be considered
  30.            be less than, equal to, or greater than the second.
  31.  
  32. E✓EX✓XA✓AM✓MP✓PL✓LE✓E
  33.        The following program sorts a simple array:
  34.            static int intcompare(const void *i, const void *j)
  35.            {
  36.              int *one, *two;
  37.  
  38.              one = (int *)i;
  39.              two = (int *)j;
  40.              return (*one - *two);
  41.            }
  42.  
  43.            void main(void)
  44.            {
  45.              int a[10];
  46.              int i;
  47.  
  48.              a[0] = 9;
  49.              a[1] = 8;
  50.              a[2] = 7;
  51.              a[3] = 6;
  52.              a[4] = 5;
  53.              a[5] = 4;
  54.              a[6] = 3;
  55.              a[7] = 2;
  56.              a[8] = 1;
  57.              a[9] = 0;
  58.  
  59.              qsort(a, 10, sizeof(int), intcompare);
  60.              for (i = 0; i < 10; i++)
  61.  
  62.  
  63.  
  64. MiNT docs 0.1              3 March 1993                         1
  65.  
  66.  
  67.  
  68.  
  69.  
  70. QSORT(3)            MINTLIB LIBRARY FUNCTIONS            QSORT(3)
  71.  
  72.  
  73.                printf(" %d", a[i]);
  74.              printf("n");
  75.            }
  76.  
  77. S✓SE✓EE✓E A✓AL✓LS✓SO✓O
  78.        b✓bs✓se✓ea✓ar✓rc✓ch✓h(✓(3✓3)✓)
  79.  
  80. N✓NO✓OT✓TE✓ES✓S
  81.        The  comparison  function  need not compare every byte, so
  82.        arbitrary data may be contained in the elements  in  addi-
  83.        tion to the values being compared.
  84.  
  85.        The  order  in  the  output  of two items which compare as
  86.        equal is unpredictable.
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130. MiNT docs 0.1              3 March 1993                         2
  131.  
  132.  
  133.