home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / EDUCATIO / NTUMIN10.ZIP / SORTING.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-09  |  3.0 KB  |  105 lines

  1. /****************************************************************************
  2.  *
  3.  *    Program Name : SORTING.C
  4.  *
  5.  *    This program sorts in ascending order, the minterms in a given array.
  6.  *
  7.  *    Returns pointer to the sorted array of minterms
  8.  *
  9.  * --------------------------------------------------------------------------
  10.  *    Copyright (c) 1992. All Rights Reserved. Nanyang Technological
  11.  *    University.
  12.  *
  13.  *    You are free to use, copy and distribute this software and its
  14.  *    documentation providing that:
  15.  *
  16.  *        NO FEE IS CHARGED FOR USE, COPYING OR DISTRIBUTION.
  17.  *
  18.  *        IT IS NOT MODIFIED IN ANY WAY.
  19.  *
  20.  *        THE COPYRIGHT NOTICE APPEAR IN ALL COPIES.
  21.  *
  22.  *    This program is provided "AS IS" without any warranty, expressed or
  23.  *    implied, including but not limited to fitness for any particular
  24.  *    purpose.
  25.  *
  26.  *    If you find NTUMIN fast, easy, and useful, a note or comment would be
  27.  *    appreciated. Please send to:
  28.  *
  29.  *        Boon-Tiong Tan or Othman Bin Ahmad
  30.  *        School of EEE
  31.  *        Nanyang Technological University
  32.  *        Nanyang Avenue
  33.  *        Singapore 2263
  34.  *        Republic of Singapore
  35.  *
  36.  ***************************************************************************/
  37.  
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include <stdlib.h>
  41.  
  42. unsigned char   *sorting(b)
  43. unsigned char     *b;
  44.  
  45. {
  46.    unsigned short  mb, i;
  47.    unsigned char   nspm, *hold1, *hold2, *temp, j;
  48.          int   test;
  49.  
  50.  
  51.    nspm = *(b+3);                             /* no. of bytes/minterm */
  52.    mb = *(b+2)<<8 | *(b+1);                   /* no. of minterms in b */
  53.  
  54.    hold1 = (unsigned char *)  malloc(nspm);   /* temporary storage */
  55.    if (hold1 == 0)
  56.       {
  57.      printf("Out of memory -- SORTING, *hold1\n");
  58.      printf("Program Terminated - 1");
  59.      exit(0);
  60.       }
  61.  
  62.    hold2 = (unsigned char *)  malloc(nspm);   /* temporary storage */
  63.    if (hold2 == 0)
  64.       {
  65.      printf("Out of memory -- SORTING, *hold2\n");
  66.      printf("Program Terminated - 2");
  67.      exit(0);
  68.       }
  69.  
  70.    temp = (unsigned char *)  malloc(nspm);   /* temporary storage */
  71.    if (temp == 0)
  72.       {
  73.      printf("Out of memory -- SORTING, *temp \n");
  74.      printf("Program Terminated - 3");
  75.      exit(0);
  76.       }
  77.  
  78.    while (mb-- > 1)                      /* bubble sort algorithm */
  79.       {
  80.      for (i=0; i<mb; i++)            /* do for decreasing order of mb */
  81.         {
  82.            for (j=nspm; j>0; j--)    /* rearrange for nspm>1 */
  83.           {
  84.              *(hold1+nspm-j) = *(b+4+nspm*i+j-1);
  85.              *(hold2+nspm-j) = *(b+4+nspm*(i+1)+j-1);
  86.           }
  87.  
  88.            test = memcmp(hold1, hold2, nspm);
  89.            if (test > 0)                            /* hold1 > hold2 */
  90.           {                                     /* swap minterms */
  91.              memcpy(temp, (b+4+nspm*i), nspm);
  92.              memcpy((b+4+nspm*i), (b+4+nspm*(i+1)), nspm);
  93.              memcpy((b+4+nspm*(i+1)), temp, nspm);
  94.           }
  95.         }
  96.       }
  97.    free(hold1);                        /* free pointers */
  98.    free(hold2);
  99.    free(temp);
  100.  
  101.    return(b);                          /* return sorted array */
  102. }
  103.  
  104.  
  105.