int in[] = {10, 32, -1, 567, 3, 18, 1, -51, 789, 0}; #define CHAR_HEIGHT 15 #define CHAR_WIDTH 8 static int cursor_x = 0, cursor_y = CHAR_HEIGHT; lputchar(char c) { switch(c) { case '\n': { cursor_x = 0; cursor_y += CHAR_HEIGHT; } break; default: { putchar(cursor_x,cursor_y,c); cursor_x += CHAR_WIDTH; } break; } } lputs(char *s) { while (*s) lputchar(*s++); } /* lputd - output decimal number */ lputd(n) { char res[12]; /* can hold decimal ascii for signed 32-bit int */ int pos = 10; int neg = 0; res[11] = '\0'; if (n < 0) { neg = 1; n = -n; } do { res[pos--] = n%10 + '0'; n = n/10; } while (n); pos++; if (neg) res[--pos] = '-'; lputs(res + pos); } main() { int i; sort(in, (sizeof in)/(sizeof in[0])); for (i = 0; i < (sizeof in)/(sizeof in[0]); i++) { lputd(in[i]); lputchar('\n'); } return 0; } int *xx; /* sort - sort a[0..n-1] into increasing order */ sort(a, n) int a[]; { quick(xx = a, 0, --n); } /* quick - quicksort a[lb..ub] */ quick(a, lb, ub) int a[]; { int k, partition(); if (lb >= ub) return; k = partition(a, lb, ub); quick(a, lb, k - 1); quick(a, k + 1, ub); } /* partition - partition a[i..j] */ int partition(a, i, j) int a[]; { int v, k; j++; k = i; v = a[k]; while (i < j) { i++; while (a[i] < v) i++; j--; while (a[j] > v) j--; if (i < j) exchange(&a[i], &a[j]); } exchange(&a[k], &a[j]); return j; } /* exchange - exchange *x and *y */ exchange(x, y) int *x, *y; { int t; /** printf("exchange(%d,%d)\n", x - xx, y - xx); **/ lputs("exchange("); lputd(x - xx); lputchar(','); lputd(y - xx); lputs(")\n"); t = *x; *x = *y; *y = t; }