sorting algorithm

This is a discussion on sorting algorithm within the C Programming forums, part of the General Programming Boards category; I am trying to make a program that takes an array and sorts it. When I try to compile this ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    28

    Question 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;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #define key(A) (A)
    #define less(A, B) (Key(A) < key(B))
    You tell me.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    I didn't even notice! Thank you.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    127
    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.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. *Help*in sorting and searching algorithm
    By yuentong in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2009, 10:43 AM
  2. Efficient Sorting Algorithm
    By GCNDoug in forum C++ Programming
    Replies: 10
    Last Post: 11-13-2008, 09:06 AM
  3. Sorting Algorithm Help
    By cjwenigma in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2007, 03:04 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. which sorting algorithm for GBs of data?
    By Sargnagel in forum C Programming
    Replies: 4
    Last Post: 06-05-2003, 09:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21