473,326 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

qsort mode-based sorting

Hello all,
Suppose we have the following struct :

struct foo {
int a;
int b;
char *c;
}

And :

struct foo bar[100]

And we want to sort bar in relation to a mode, for example if mode == 1 then
sort bar by comparing a member, if mode == 2 then sort bar by comparing b
members and so on. The mode variable cannot be used as extern and qsort()
prototype needs a function compar() which takes two void * arguments. Is
there any way to pass the mode variable to compar() or I have to write my
own qsort() ?

D.

--
PGP key 39A40276 at wwwkeys.eu.pgp.net
key fingeprint: 9A0B 61C6 B826 4B73 69BB 972B C5E7 A153 39A4 0276
Nov 14 '05 #1
6 2108
Dimitris Mandalidis <ma****@mandas.org.remove_this_to_reply> wrote:
Suppose we have the following struct : struct foo {
int a;
int b;
char *c;
} And : struct foo bar[100] And we want to sort bar in relation to a mode, for example if mode == 1 then
sort bar by comparing a member, if mode == 2 then sort bar by comparing b
members and so on. The mode variable cannot be used as extern and qsort()
prototype needs a function compar() which takes two void * arguments. Is
there any way to pass the mode variable to compar() or I have to write my
own qsort() ?


When you can't use an external variable for the mode (and you don't
want to stick a 'mode' member in all structures and keep that ip-to-
date) then you need to write your own qsort(). On the other hand, why
don't you write two comparison functions and call qsort() with the
first one for the first mode and with the second one for the other?
Looks to me like the most natural way.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2

"Dimitris Mandalidis" <ma****@mandas.org.remove_this_to_reply> wrote
[ sorting by different comparison functions depending on mode ]
Is
there any way to pass the mode variable to compar() or I have to
write my own qsort() ?

You'd be crazy to write your own qsort() unless it is as a learning
exercise. However it is not possible to pass an arbitrary pointer to the
comparison function except by including it in the structure to be sorted.
The obvious way round is to write several comparison functions and call
qsort() with the appropriate one.
The other solution is to make the mode variables static and put the
comparsion function in its own file with a "setmode" function. This is not
thread safe but doesn't have any of the namespace disadvantages of using
global variables.

Nov 14 '05 #3
Dimitris Mandalidis <ma****@mandas.org.remove_this_to_reply> writes:
And we want to sort bar in relation to a mode, for example if mode == 1 then
sort bar by comparing a member, if mode == 2 then sort bar by comparing b
members and so on. The mode variable cannot be used as extern and qsort()
prototype needs a function compar() which takes two void * arguments. Is
there any way to pass the mode variable to compar() or I have to write my
own qsort() ?


In the general case where you want to pass some auxiliary data to
a qsort() comparison function, you're probably best off writing a
qsort() variant that allows passing auxiliary data. In my own
programs I sometimes use such a function, which I built by taking
the qsort() implementation from GNU libc and adding an auxiliary
data argument.

In this specific case where there are only a few possibilities,
just use multiple comparison functions, e.g.
qsort (array, sizeof array / sizeof *array, sizeof *array,
mode == 1 ? func_1 : (mode == 2 ? func_2 : func_3));
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #4
Thank you all for answering to my question, I got the point :-)

D.

--
PGP key 39A40276 at wwwkeys.eu.pgp.net
key fingeprint: 9A0B 61C6 B826 4B73 69BB 972B C5E7 A153 39A4 0276
Nov 14 '05 #5
In article Ben Pfaff <bl*@cs.stanford.edu> says...
Dimitris Mandalidis <ma****@mandas.org.remove_this_to_reply> writes:
And we want to sort bar in relation to a mode, for example if mode == 1 then
sort bar by comparing a member, if mode == 2 then sort bar by comparing b
members and so on. The mode variable cannot be used as extern and qsort()
prototype needs a function compar() which takes two void * arguments. Is
there any way to pass the mode variable to compar() or I have to write my
own qsort() ?


In the general case where you want to pass some auxiliary data to
a qsort() comparison function, you're probably best off writing a
qsort() variant that allows passing auxiliary data. In my own
programs I sometimes use such a function, which I built by taking
the qsort() implementation from GNU libc and adding an auxiliary
data argument.

In this specific case where there are only a few possibilities,
just use multiple comparison functions, e.g.
qsort (array, sizeof array / sizeof *array, sizeof *array,
mode == 1 ? func_1 : (mode == 2 ? func_2 : func_3));

<delurks>
Wouldn't an array of function pointers, indexed into with mode, be nicer?
--
Kushal Kumaran <ku************@hotpop.net>
Nov 14 '05 #6
Kushal Kumaran wrote:

In article Ben Pfaff <bl*@cs.stanford.edu> says...
Dimitris Mandalidis <ma****@mandas.org.remove_this_to_reply> writes:
And we want to sort bar in relation to a mode, for example if mode == 1 then
sort bar by comparing a member, if mode == 2 then sort bar by comparing b
members and so on. The mode variable cannot be used as extern and qsort()
prototype needs a function compar() which takes two void * arguments. Is
there any way to pass the mode variable to compar() or I have to write my
own qsort() ?


In the general case where you want to pass some auxiliary data to
a qsort() comparison function, you're probably best off writing a
qsort() variant that allows passing auxiliary data. In my own
programs I sometimes use such a function, which I built by taking
the qsort() implementation from GNU libc and adding an auxiliary
data argument.

In this specific case where there are only a few possibilities,
just use multiple comparison functions, e.g.
qsort (array, sizeof array / sizeof *array, sizeof *array,
mode == 1 ? func_1 : (mode == 2 ? func_2 : func_3));

<delurks>
Wouldn't an array of function pointers, indexed into with mode,
be nicer?


I think it would be.

--
pete
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: richard | last post by:
What might cause qsort to crash? I'm using qsort to sort a few thousand items of a not too complex structure. Tried it with one data set - worked fine. Tried another similarly sized data set...
11
by: William Buch | last post by:
I have a strange problem. The code isn't written by me, but uses the qsort function in stdlib. ALWAYS, the fourth time through, the memory location of variable list (i.e. mem location = 41813698)...
5
by: Steve | last post by:
can someone tell me how qsort function in <stdlib.h> is used (qsort(..........))? the function has a buffer, two void * parameters and the a pointer to a compare function. Thanks.
7
by: Excluded_Middle | last post by:
Suppose I have a struct typdef struct foo { int age; char *name; }foo; now I made a list of foo using
17
by: Trent Buck | last post by:
The fourth argument is a comparator that returns `an integer less than, equal to, or greater than zero' depending on the ordering of its arguments. If I don't care about the order and simply...
32
by: John Smith | last post by:
I'm trying to figure out qsort(). I haven't seen any practical examples, only synopsis. In the code below, the array is not sorted. Can someone give me some help? #include <stdio.h> #include...
3
by: No Such Luck | last post by:
Hi All: The code below (using the qsort function) produces the following incorrect result. The last two numbers are not sorted. It this innaccurate result specific to my compiler's qsort, or is...
5
by: Bidule | last post by:
Hi, I'm trying to sort structs defined as follows: struct combinationRec { float score; char* name; }; The number of structs and the length of the "name" field are not known
10
by: gauss010 | last post by:
Suppose I have an object A of type char. Each A is a buffer containing a string, and I want to sort the M strings of A using the strcmp function. The description of the qsort function says that I...
61
by: Ron Ford | last post by:
K&R has three different versions of qsort, and the ultimate one is supposed to be like the one in the std library. I'm trying to implement the first, which is in §4.10. I think I'm pretty close...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.