Date: Fri, 14 Jun 2002 08:45:30 -0400 From: khromy@lnuxlab.ath.cx (khromy) To: echo dev <echo_dev@hotmail.com> Cc: hackers@freebsd.org Subject: Re: sorting in C Message-ID: <20020614124530.GA16778@lnuxlab.ath.cx> In-Reply-To: <F32bek2lisuJnOVHKgG00003ca2@hotmail.com> References: <F32bek2lisuJnOVHKgG00003ca2@hotmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
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 majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020614124530.GA16778>
