Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Examples  

test_ext_sort.c

This file shows how to use the extended_qksort() function.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "sort.h"

/* -------------------------------------------------------------- */
/* User defined data (that will appear in the array)              */
/* -------------------------------------------------------------- */

struct s_data {
                 char *s;
                 int  value;
              };

/* -------------------------------------------------------------- */
/* User defined function used to copy data from/to the array      */
/*                                                                */
/*        WARNING: The **FIRST** element is the SOURCE.           */
/*                 The **SECOND** element is the DESTINATION.     */
/*                                                                */
/*   *** THIS FUNCTION MUST TAKE CARE OF MEMORY MANAGEMENT ***    */
/* -------------------------------------------------------------- */

int copy_data(void *src, void *dest)
{
  struct s_data *from, *to;

  from = (struct s_data*)src;
  to   = (struct s_data*)dest;

  if (to->s != NULL) { free(to->s); }

  to->s = (char*)malloc((strlen(from->s)+1)*sizeof(char));
  if (to->s == NULL) { return 1; }

  strcpy (to->s, from->s);
  to->value = from->value;

  return 0;
}

/* -------------------------------------------------------------- */
/* User defined function used to compare two elements of the      */
/* array                                                          */
/* -------------------------------------------------------------- */

int compare(const void *d1, const void *d2)
{
  struct s_data *data1, *data2;

  data1 = (struct s_data*)d1;
  data2 = (struct s_data*)d2;

  if (data1->value > data2->value) { return 1; }
  if (data1->value < data2->value) { return -1; } 

  return 0;
}

/* -------------------------------------------------------------- */
/* User defined function used to initialize one element           */
/* -------------------------------------------------------------- */

int init_elem (void* elm)
{
  struct s_data *data;

  data = (struct s_data*)elm;
  data->s     = NULL;
  data->value = 0;

  return 0;
}



/* -------------------------------------------------------------- */
/* Program's entry point                                          */
/* -------------------------------------------------------------- */

int main (int argc, char *argv[])
{
  int             nb_elem, i, v, cr;
  struct s_data   *table;
  char            buff[32];


  /*------------------------------------------------------------- */
  /* Check argument line                                          */
  /*------------------------------------------------------------- */

  if (argc != 2)
  {
    fprintf (stderr, "\nUsage: ext_sort.test <number of elements>\n");
    return 1;
  }

  nb_elem = atoi(argv[1]);

  /*------------------------------------------------------------- */
  /* Allocate memory                                              */
  /*------------------------------------------------------------- */

  table = (struct s_data*)malloc(nb_elem * sizeof(struct s_data));
  if (table == NULL)
  {
    fprintf (stderr, "\nERROR: Can nor allocate memory\n");
    return 1;
  }

  /*------------------------------------------------------------- */
  /*                                                              */
  /*                       TESTING ISSORT                         */
  /*                                                              */
  /*------------------------------------------------------------- */

  /*------------------------------------------------------------- */
  /* Put random data into the array                               */
  /*------------------------------------------------------------- */

  for (i=0; i<nb_elem; i++)
  {
    v = rand();
    sprintf (buff, "%d", v);

    table[i].s = (char*)malloc(strlen(buff)+1);
    if (table[i].s == NULL)
    {
      fprintf (stderr, "\nERROR: Can nor allocate memory\n");
      return 1;
    }
    strcpy (table[i].s, buff);

    table[i].value = v;
  }

  /*------------------------------------------------------------- */
  /* Print the list of elements                                   */
  /*------------------------------------------------------------- */

  fprintf (stdout, "\n");
  fprintf (stdout, "\nElements to sort:");
  fprintf (stdout, "\n-----------------");
  fprintf (stdout, "\n");
  fflush (stdout);

  for (i=0; i<nb_elem; i++)
  {
    fprintf (stdout, "\n%s", table[i].s);
  }

  /*------------------------------------------------------------- */
  /* Sorting the elements now                                     */
  /*------------------------------------------------------------- */

  cr = extended_issort (
                         (void*)table,
                         nb_elem,
                         sizeof(struct s_data),
                         compare,
                         copy_data,
                         init_elem
                       );

  if (cr == -1)
  {
    fprintf (stderr, "\nERROR: Can sort data using extended issort()\n");
    return 1;
  }

  /*------------------------------------------------------------- */
  /* Print the list of elements                                   */
  /*------------------------------------------------------------- */

  fprintf (stdout, "\n");
  fprintf (stdout, "\nSorted elements:");
  fprintf (stdout, "\n---------------");
  fprintf (stdout, "\n");
  fflush (stdout);

  for (i=0; i<nb_elem; i++)
  {
    fprintf (stdout, "\n%s", table[i].s);
  }

  /*------------------------------------------------------------- */
  /* Free all memory                                              */
  /*------------------------------------------------------------- */

  for (i=0; i<nb_elem; i++)
  {
    if (table[i].s != NULL) { free (table[i].s); }
  }

  /*------------------------------------------------------------- */
  /*                                                              */
  /*                       TESTING QKSORT                         */
  /*                                                              */
  /*------------------------------------------------------------- */

  /*------------------------------------------------------------- */
  /* Put random data into the array                               */
  /*------------------------------------------------------------- */

  for (i=0; i<nb_elem; i++)
  {
    v = rand();
    sprintf (buff, "%d", v);

    table[i].s = (char*)malloc(strlen(buff)+1);
    if (table[i].s == NULL)
    {
      fprintf (stderr, "\nERROR: Can nor allocate memory\n");
      return 1;
    }
    strcpy (table[i].s, buff);

    table[i].value = v;
  }

  /*------------------------------------------------------------- */
  /* Print the list of elements                                   */
  /*------------------------------------------------------------- */

  fprintf (stdout, "\n");
  fprintf (stdout, "\nElements to sort:");
  fprintf (stdout, "\n-----------------");
  fprintf (stdout, "\n");
  fflush (stdout);

  for (i=0; i<nb_elem; i++)
  {
    fprintf (stdout, "\n%s", table[i].s);
  }

  /*------------------------------------------------------------- */
  /* Sorting the elements now                                     */
  /*------------------------------------------------------------- */

  cr = extended_qksort (
                         (void*)table,
                         nb_elem,
                         sizeof(struct s_data),
                         0,
                         nb_elem-1,
                         compare,
                         copy_data,
                         init_elem
                       );

  if (cr == -1)
  {
    fprintf (stderr, "\nERROR: Can sort data using extended qssort()\n");
    return 1;
  }

  /*------------------------------------------------------------- */
  /* Print the list of elements                                   */
  /*------------------------------------------------------------- */

  fprintf (stdout, "\n");
  fprintf (stdout, "\nSorted elements:");
  fprintf (stdout, "\n---------------");
  fprintf (stdout, "\n");
  fflush (stdout);

  for (i=0; i<nb_elem; i++)
  {
    fprintf (stdout, "\n%s", table[i].s);
  }

  /*------------------------------------------------------------- */
  /* Free all memory                                              */
  /*------------------------------------------------------------- */

  for (i=0; i<nb_elem; i++)
  {
    if (table[i].s != NULL) { free (table[i].s); }
  }

  free (table);

  return 0;
}


Generated on Thu Apr 3 16:23:40 2003 for Common_C_libraries by doxygen1.3-rc1