Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ qsort(3C) — DG/UX 4.00

Media Vault

Software Library

Restoration Projects

Artifacts Sought



                                                                qsort(3C)



        _________________________________________________________________
        qsort                                                    function
        Sort an array of items.
        _________________________________________________________________


        Calling Sequence

        void qsort();
        char *base;
        unsigned numelements, sizeofelement;
        int (*compare)();
        qsort(base, numelements, sizeofelement, compare);


          where   base is a pointer to the array start, which you must
                  cast as a pointer to a character (char *).
                  numelements is the number of elements in the array.
                  sizeofelement is the size, in bytes, of each
                  element in the array.
                  compare is the address of an int function, which
                  takes two arguments:

                  arg1 Pointer to the first element, cast as (char *).
                  arg2 Pointer to the second element, cast as (char
                  *).



        Description

        The qsort function sorts an array of items, using the quicker-
        sort algorithm.  It determines the sorting order of individual
        elements with a user-specified function.  This qsort function is
        compatible with the routine in the UNIX operating system with the
        same name.

        Because of hardware restrictions, you must explicitly cast the
        array you pass as a character pointer.  In addition, you must
        call the function compare with two character pointers.  If the
        pointers are actually of some other type, such as integer
        pointers, you must cast them to the correct pointer type from the
        character pointer within the function.


        Returns

        The return value for compare is a negative integer if the first
        element is smaller than the second integer, a positive integer if
        the first element is greater than the second, or 0 if the two are
        equal.



        DG/UX 4.00                                                 Page 1
               Licensed material--property of copyright holder(s)





                                                                qsort(3C)



        Related Functions

        See also the function bsearch.


        Example

        The following program reads in integers, sorts them, and then
        prints them out.
        void qsort();

        #define MAX_ELE 200      /* maximum # elements */

        int array[MAX_ELE], *pi = array, num = 0;

        /*  compare function -- compares two integers for qsort  */

        int compare(pnum1, pnum2)
        char *pnum1, *pnum2; /* qsort always passes (char *) */
        {
            int num1, num2;

            num1 = *(int *)pnum1; /* cast is essential! */
            num2 = *(int *)pnum2;

            if(num1 < num2)
                return -1;
            else if(num1 == num2)
                return 0;
            else    return 1;
        }

        /* main routine -- read, sort, and print numbers.  */

        main() {
            int i;
                /* loop reading the numbers */
            while( (pi <= &array[MAX_ELE-1]) &&
                   (scanf( "%d", pi) == 1)) {
                pi++;
                num++;
            }
            qsort((char *)array, num, sizeof(int), compare);
            printf("The sorted numbers are:");
            for(pi = array, i = 0; i < num; i++)
                printf("%11d", *pi++);
        }







        DG/UX 4.00                                                 Page 2
               Licensed material--property of copyright holder(s)



Typewritten Software • bear@typewritten.org • Edmonds, WA 98026