Chapter Contents

Previous

Next
qsort

qsort



Sort an Array of Elements

Portability: ISO/ANSI C conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <stdlib.h>

void qsort(void *blk, size_t n, size_t size,
           int (*cmp)(const void *, const void *));


DESCRIPTION

qsort sorts the array pointed to by blk , using the quicksort algorithm. n is the number of elements in the array. size is the element size in bytes. cmp is a user-provided comparison function.

qsort calls cmp with pointers to two elements of the array. cmp determines which element is larger or whether the two elements are equal. The cmp function thereby defines the ordering relation for the elements to be sorted. The precise comparison technique that should be implemented by the cmp function depends on the type of data to be compared and on the application. A typical comparison function is illustrated under EXAMPLE below.

cmp returns these values:

If the two elements are equal, their order after sorting is arbitrary.


RETURN VALUE

qsort has no return value.


EXAMPLE

#include <stdlib.h>
#include <stdio.h>

#define MAXEMPLOY 100
#define EMPLOYEE_FILENAME "employee"

typedef struct {            /* Define employee record. */
   unsigned employ_no;
   char last_name[30];
}  employee;
employee emp_tabl[MAXEMPLOY];

   /* Compare function for bsearch and qsort.          */
static int compare_employees(const void *, const void *);

main()
{
   FILE *employ_file;
   int employ_count = 0;
   unsigned srchid;     /* Search value (employee id). */
   char *temp_emp;

   employ_file = fopen(EMPLOYEE_FILENAME, "rb");

      /* Error checking omitted.                       */
      /* Read in employee file.                        */
   while (!feof(employ_file)) {
      fread(&emp_tabl[employ_count], sizeof(employee),
            1, employ_file);
      ++employ_count;
   }
   fclose(employ_file);
      /* Sort employee table by employee number.       */
   qsort(emp_tabl, employ_count, sizeof(employee),compare_employees);

   puts("Enter Employee ID to Search for:");
   scanf("%d", &srchid);         /* Enter search data. */

      /* Do a lookup with bsearch for an entry in the  */
      /*  employee table. It uses the same             */
      /*  compare_employees function.                  */
   temp_emp = bsearch(&srchid, emp_tabl, employ_count,
                   sizeof(employee), &compare_employees);
   if (temp_emp == NULL)   /* Print results of search. */
      printf("Invalid ID\n");
   else
      printf("Last Name: %s\n",
            ((employee *) temp_emp)->last_name);
}

static int compare_employees(const void *first,const void *second)
{
   employee *efirst, *esecond;

   efirst  = (employee *)first;
   esecond = (employee *)second;

      /* Return -1 if first emp no < second emp, 0 if  */
      /* they are equal, or 1 if first > second.       */
   return (efirst->employ_no > esecond->employ_no) -
          (efirst->employ_no < esecond->employ_no);
}


RELATED FUNCTIONS

bsearch


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.