From owner-freebsd-hackers Fri Jun 14 5:41:27 2002 Delivered-To: freebsd-hackers@freebsd.org Received: from mtiwmhc21.worldnet.att.net (mtiwmhc21.worldnet.att.net [204.127.131.46]) by hub.freebsd.org (Postfix) with ESMTP id 315CB37B414 for ; Fri, 14 Jun 2002 05:41:23 -0700 (PDT) Received: from lnuxlab.ath.cx ([12.77.148.124]) by mtiwmhc21.worldnet.att.net (InterMail vM.4.01.03.27 201-229-121-127-20010626) with ESMTP id <20020614124116.DQYZ19182.mtiwmhc21.worldnet.att.net@lnuxlab.ath.cx>; Fri, 14 Jun 2002 12:41:16 +0000 Received: by lnuxlab.ath.cx (Postfix, from userid 1000) id D4CC81397A; Fri, 14 Jun 2002 08:45:30 -0400 (EDT) Date: Fri, 14 Jun 2002 08:45:30 -0400 To: echo dev Cc: hackers@freebsd.org Subject: Re: sorting in C Message-ID: <20020614124530.GA16778@lnuxlab.ath.cx> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.3.28i From: khromy@lnuxlab.ath.cx (khromy) Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG 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 #include #include #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