# sorting algorithm

• 05-04-2004
RazielX
sorting algorithm
I am trying to make a program that takes an array and sorts it. When I try to compile this program, I get a message saying "undefined reference to "_Key"." What am I doing wrong?

sort.h
Code:

```#include<stdio.h> #include<stdlib.h> #include<time.h> #define N 100 #define key(A) (A) #define less(A, B) (Key(A) < key(B)) #define exch(A, B) {int t = A; A = B; B = t;} #define compexch(A, B) if(less(B, A)) exch(A, B) void random_array(int a[]); void sorted_array(int a[]); void asorted_array(int a[]); void selection(int a[], int l, int r);```
sort.c
Code:

```#include"sort.h" void random_array(int a[]) {         int i;         for(i=0; i<N; i++)                 a[i] = rand(); } void sorted_array(int a[]) {         int i;         for(i=0; i<N; i++)                 a[i] = i+1; } void asorted_array(int a[]) {         int i;         for(i=0; i<N; i++)                 a[i] = i;         for(i=0; i<N; i+=10)                 a[i] = rand(); } void selection(int a[], int l, int r) {         int i, j;         for(i = l; i < r; i++) {                 int min = i;                 for(j = i+1; j <= r; j++)                         if(less(a[j], a[min])) min = j;                 exch(a[i], a[min]);         } }```
sortdriver.c
Code:

```#include"sort.c" int main() {         int a[N], i;         srand((unsigned) time(NULL));         random_array(a);         selection(a, 0, N);                 for(i=0;i<10;i++)                 printf("%d ", a[i]);         printf("\n");         return 0; }```
• 05-04-2004
quzah
Code:

```#define key(A) (A) #define less(A, B) (Key(A) < key(B))```
You tell me.

Quzah.
• 05-04-2004
RazielX
I didn't even notice! Thank you.
• 05-04-2004
Kip Neuhart
Be cautious when using Robert Sedgewick's code. It's a poor translation from Pascal, and a lot of it won't compile directly out of the book.
• 05-04-2004
RazielX
I am actually having trouble with his quicksort and mergesort algorithm right now. The quicksort won't sort right (and won't even work when I try to sort characters), and the mergesort compiles, but crashes the cmd window I run the program in.