Chapter 8. The Preprocessor

Home

Bubble Sort

Bi-Directional Bubble Sort Quick Sort  
alt="Your browser understands the <APPLET> tag but isn't running the applet, for some reason."Your browser is completely ignoring the <APPLET> tag! alt="Your browser understands the <APPLET> tag but isn't running the applet, for some reason."Your browser is completely ignoring the <APPLET> tag! alt="Your browser understands the <APPLET> tag but isn't running the applet, for some reason."Your browser is completely ignoring the <APPLET> tag!

                       

PROGRAM 81 : try_qsort.c

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

#define N 11 /* size of the array */

enum when {before, after};

typedef enum when when;

int cmp(const void *vp, const void *vq);
/* comparison fct */
void fill_array(double *a, int n);
void prn_array(when val, double *a, int n);

int main(void)
{
  double a[N];

  fill_array(a, N);
  prn_array(before, a, N);
  qsort(a, N, sizeof(double), cmp);
  prn_array(after, a, N);
  return 0;
}

int cmp(const void *vp, const void *vq)
{
  const double *p = vp;
  const double *q = vq;
  double diff = *p - *q;

  return ((diff >= 0.0) ? ((diff > 0.0) ? -1 : 0) : +1); 


void fill_array(double *a, int n)
{
  int i;

  srand(time(NULL)); /* seed rand() */
  for (i = 0; i < n; ++i)
    a[i] = (rand() % 1001) / 10.0;
}

void prn_array(when val, double *a, int n)
{
  int i;

  printf("%s\n%s%s\n",
    "---", 
    ((val == before) ? "Before " : "After "), "sorting:");
  for (i = 0; i < n; ++i) {
    if (i % 6 == 0) putchar('\n');
    printf("%10.1f", a[i]);
  }
  putchar('\n'); 
}

กก

PROGRAM 82 : main.c

#include "sort.h"

int main(void)
{
  char a[M];
  float b[N];
  int i;

  srand(time(NULL));
  FILL(a, M , "char");
  PRINT(a, M, "%-2c");
  qsort(a, M, sizeof(char), lexico);
  PRINT(a, M, "%-2c");
  printf("---\n");
  FILL(b, N, "float");
  PRINT(b, N, "%-6.1f");
  qsort(b, N, sizeof(float), compare_fractional_part);
  PRINT(b, N, "%-6.1f");
  return 0;
}

PROGRAM 822 : compare.c

#include "sort.h"

int compare_fractional_part(const void *vp, const void *vq)
{
  const float *p = vp, *q = vq;
  float x;

  x = fractional_part(*p) - fractional_part(*q);
  return ((x < 0.0) ? -1 : (x == 0.0) ? 0 : +1);
}

int lexico(const void *vp, const void *vq)
{
  const char *p = vp, *q = vq;
  return (*p - *q);
}

PROGRAM 823 : sort.h

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

#define M 32 /* size of a[] */
#define N 11 /* size of b[] */
#define fractional_part(x) (x - (int) x)
#define random_char() (rand() % 26 + 'a')
#define random_float() (rand() % 100 / 10.0)

#define FILL(array, sz, type) \
  if (strcmp(type, "char") == 0) \
    for (i = 0; i < sz; ++i) \
      array[i] = random_char(); \
  else \
    for (i = 0; i < sz; ++i) \
      array[i] = random_float()

#define PRINT(array, sz, cntrl_string) \
  for (i = 0; i < sz; ++i) \
    printf(cntrl_string, array[i]); \
  putchar('\n')

int compare_fractional_part(const void *, const void *);
int lexico(const void *, const void *);

PROGRAM 83 : apple_pie.c

#include <stdio.h>

#undef PIE
#define PIE "I like apple."

int main(void)
{
  printf("PIE: " PIE "\n");
  return 0;
}

PROGRAM 84 : stringization.c

#include <stdio.h>

#define message_for(a, b) \
printf(#a " and " #b ": We love you!\n")

int main(void)
{
  message_for(Carole, Debra);
  return 0;
}

PROGRAM 85 : assert.c

#include <stdio.h>
#include <stdlib.h> /* for abort() */

#if defined(NDEBUG)
#define assert(ignore) ((void) 0) /* ignore it */
#else
#define assert(expr) \
  if (!(expr)) { \
    printf("\n%s%s\n%s%s\n%s%d\n\n", \
      "Assertion failed: ", #expr, \
      "in file ", __FILE__, \
      "at line ", __LINE__); \
      abort(); \
  }
#endif

int main(void)
{
  assert(1 > 2);
  return 0;
}

PROGRAM 86 : quicksort.c

/* Quicksort! Pointer version with macros. */

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

#define N 300

#define swap(x, y) { int t; t = x; x = y; y = t; }
#define order(x, y) if (x > y) swap(x, y)
#define o2(x, y) order(x, y)
#define o3(x, y, z) o2(x, y); o2(x, z); o2(y, z)

typedef enum {yes, no} yes_no;

static yes_no find_pivot(int *left, int *right, int *pivot_ptr);
static int *partition(int *left, int *right, int pivot);
void quicksort(int *, int *);

int main(void)
{
  int a[N], i;

  srand(time(NULL));
  for (i = 0; i < N; ++i)
    a[i] = rand() % 1000;
  quicksort(a, a + N - 1);
  for (i = 0; i < 7; ++i)
    printf("%4d", a[i]);
  printf(" .....");
  for (i = N - 8; i < N; ++i)
    printf("%4d", a[i]);
  putchar('\n');
  return 0;
}

static yes_no find_pivot(int *left, int *right, int *pivot_ptr)
{
  int a, b, c, *p;

  a = *left; /* left value */
  b = *(left + (right - left) / 2); /* middle value */
  c = *right; /* right value */
  o3(a, b, c); /* order these 3 values */
  if (a < b) { /* pivot will be higher of 2 values */
    *pivot_ptr = b;
    return yes;
  }
  if (b < c) {
    *pivot_ptr = c;
    return yes;
  }
  for (p = left + 1; p <= right; ++p)
    if (*p != *left) {
      *pivot_ptr = (*p < *left) ? *left : *p;
      return yes;
  }
  return no; /* all elements have the same value */
}

static int *partition(int *left, int *right, int pivot)
{
  while (left <= right) {
    while (*left < pivot)
      ++left;
    while (*right >= pivot)
      --right;
    if (left < right) {
      swap(*left, *right);
      ++left;
      --right;
    }
  }
  return left;
}

void quicksort(int *left, int *right)
{
  int *p, pivot;

  if (find_pivot(left, right, &pivot) == yes) {
    p = partition(left, right, pivot);
    quicksort(left, p - 1);
    quicksort(p, right);
  }
}

กก


[Last Update: 2001.5.25] Dongseo University Cyber Campus