On Fri, Jun 14, 2002 at 07:06:06AM +0000, echo dev wrote:
> I am pooling in as many different ways of sorting data in C i can anyone 
> have a fav??? If anyone can give me some ideas on the best way to sort data 
> in C would be helpful.. Thanks

Below is an example of how to use qsort.  Hope it helps.

/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */

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

#define NUM_INTS 15

void print_array(int *array)
{
        int i;
        for (i = 0; i < NUM_INTS; i++) {
                printf("%d ", array[i]);
        }
        putc('\n', stdout);
}

int comp(int *a, int *b)
{
        return (*a - *b);
}

void gen_rand(int *array)
{
        int i;
        srand(time(NULL));

        for (i = 0; i < NUM_INTS; i++) {
                array[i] = rand() % 99;
        }
}

int main(void)
{
        int array[NUM_INTS];

        gen_rand(array);

        print_array(array);
        qsort(&array, NUM_INTS, sizeof(*array),
                (int (*)(const void *, const void *)) comp);
        print_array(array);
        putc('\n', stdout);

        return 0;
}

-- 
L1:     khromy          ;khromy(at)lnuxlab.ath.cx

To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message

Reply via email to