Re: sorting in C

2002-06-14 Thread Alfred Perlstein

* echo dev [EMAIL PROTECTED] [020614 00:06] 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

man qsort

-- 
-Alfred Perlstein [[EMAIL PROTECTED]]

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



Re: sorting in C

2002-06-14 Thread Terry Lambert

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

The Art Of Computer Programming
Volume 3: Sorting and Searching
Donald Knuth
Addison-Wesley
ISBN: 0-201-03803-X

Knuth's books: Collect them all!  Be the first on your block!

-- Terry

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



Re: sorting in C

2002-06-14 Thread David Schultz

Thus spake echo dev [EMAIL PROTECTED]:
 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

I've always been partial to bogosort.

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



Re: sorting in C

2002-06-14 Thread Alex Belits

On Fri, 14 Jun 2002, David Schultz wrote:

 Thus spake echo dev [EMAIL PROTECTED]:
  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

 I've always been partial to bogosort.

  Hey, don't be _that_ mean to a poor Hotmail user -- maybe he got that
address before W2K, layers of Local Director and Passport.

  OTOH, asking things like that here still deserves some mockery...

-- 
Alex


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



Re: sorting in C

2002-06-14 Thread khromy

On Fri, Jun 14, 2002 at 07:06:06AM +, 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



Re: sorting in C

2002-06-14 Thread Dan Arlow

dumb question: extra  in the previous post?

why do both this
 qsort(array, NUM_INTS, sizeof(*array),
 (int (*)(const void *, const void *)) comp);
and this
  qsort(array, NUM_INTS, sizeof(*array),
  (int (*)(const void *, const void *)) comp);

work properly? I tried both and both compile/run/sort fine.

(the difference afaik is that the first passes an *int[] aka int** and the
second passes an int[] aka int*)

-dan (noob)


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



Re: sorting in C

2002-06-14 Thread Nicolas Rachinsky

* Dan Arlow [EMAIL PROTECTED] [2002-06-14 14:40 -0400]:
 dumb question: extra  in the previous post?
 
 why do both this
  qsort(array, NUM_INTS, sizeof(*array),
  (int (*)(const void *, const void *)) comp);
 and this
   qsort(array, NUM_INTS, sizeof(*array),
   (int (*)(const void *, const void *)) comp);
 
 work properly? I tried both and both compile/run/sort fine.
 
 (the difference afaik is that the first passes an *int[] aka int** and the
 second passes an int[] aka int*)

No, see http://www.eskimo.com/~scs/C-faq/q6.12.html

Nicolas

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



Re: sorting in C

2002-06-14 Thread Brian T . Schellenberger

On Friday 14 June 2002 06:36 pm, Nicolas Rachinsky wrote:
| * Dan Arlow [EMAIL PROTECTED] [2002-06-14 14:40 -0400]:
|  dumb question: extra  in the previous post?
| 
|  why do both this
| 
|   qsort(array, NUM_INTS, sizeof(*array),
|   (int (*)(const void *, const void *)) comp);
| 
|  and this
|qsort(array, NUM_INTS, sizeof(*array),
|(int (*)(const void *, const void *)) comp);
| 
|  work properly? I tried both and both compile/run/sort fine.
| 
|  (the difference afaik is that the first passes an *int[] aka int** and
|  the second passes an int[] aka int*)
|
| No, see http://www.eskimo.com/~scs/C-faq/q6.12.html

I think that probably doesn't clarify much.  (Indeed, in this case, it would 
seem to obfuscate the above question more than clarify since the question is 
why are they the same and the FAQ question is meant to explain why these 
two are *different* to somebody who expects them to be the *same.*)


It's because an array *is* a pointer to the first element, and array is a 
pointer to the array, but the first element has the same address as the 
array, since the address of the array is the address of the first element.  
Thus, the code works as long as the compiler will accept it.

If qsort had a typed first element, then you'd get an error message from 
one or the other of them (though it would probably work), but as it happens, 
qsort declares the type to be void *, so it will take *any* pointer type. 
Thus, no complaint from the compiler.

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

-- 
Brian T. Schellenberger . . . . . . .   [EMAIL PROTECTED] (work)
Brian, the man from Babble-On . . . .   [EMAIL PROTECTED] (personal)
http://www.babbleon.org

http://www.eff.org  http://www.programming-freedom.org 

If you smell the smoke you don't need to be told what you've got to do;
Yet there's a certain breed, so very in-between, they'd rather take a
vote.   -- DEVO  --  Here To Go

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



Re: sorting in C

2002-06-14 Thread Cyrille Lefevre

On Fri, Jun 14, 2002 at 07:06:06AM +, 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

is that references helping you ?

qsort(3), bsearch(3), hsearch(3), lsearch(3), db(3).

Cyrille.
-- 
Cyrille Lefevre mailto:[EMAIL PROTECTED]

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