473,511 Members | 17,486 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question on qsort library function

What is meant by stable qsort ?

Mar 3 '07 #1
14 2565
In article <11**********************@n33g2000cwc.googlegroups .com>,
su**************@yahoo.com, India <su**************@yahoo.comwrote:
>What is meant by stable qsort ?
A stable sort algorithm is one which leaves unchanged the order of
items that compare equal. C's qsort() is not guaranteed to do this.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 3 '07 #2
"su**************@yahoo.com, India" wrote:
>
What is meant by stable qsort ?
If qsort is implemented via quicksort, it is not stable. Stable
means that equal keys appear in the output in the order in which
they appeared in the input. A stable O(nLOGn) sort is mergesort.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

Mar 3 '07 #3
CBFalconer <cb********@yahoo.comwrites:
"su**************@yahoo.com, India" wrote:
>What is meant by stable qsort ?

If qsort is implemented via quicksort, it is not stable. Stable
means that equal keys appear in the output in the order in which
they appeared in the input. A stable O(nLOGn) sort is mergesort.
There are variants of Quicksort that are stable (at some cost in
speed).

Note that there is no implication in the standard that qsort() is an
implementation of the Quicksort algorithm; it could even be Bubblesort
or Bogosort in a sufficiently perverse implementation. You can
reasonably expect qsort() to use an O(n log n) algorithm. (If you're
curious, you can even investigate its internal behavior by adding
tracing code to your compar() routine.)

(Hmm, I wonder why it's called compar() rather than compare().
Historical reasons, I suppose.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 3 '07 #4
Keith Thompson wrote:
>
.... snip ...
>
(Hmm, I wonder why it's called compar() rather than compare().
Historical reasons, I suppose.)
I suspect the famous 6 char external linkage name limit.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Mar 4 '07 #5
CBFalconer <cb********@yahoo.comwrites:
Keith Thompson wrote:
>>
... snip ...
>>
(Hmm, I wonder why it's called compar() rather than compare().
Historical reasons, I suppose.)

I suspect the famous 6 char external linkage name limit.
Sure, but parameter names don't have external linkage.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 4 '07 #6
On Mar 4, 8:57 am, Keith Thompson <k...@mib.orgwrote:
(Hmm, I wonder why it's called compar() rather than compare().
Historical reasons, I suppose.)
Perhaps you could creat a better example?

Mar 4 '07 #7
Keith Thompson <ks***@mib.orgwrote:
CBFalconer <cb********@yahoo.comwrites:
Keith Thompson wrote:
>
(Hmm, I wonder why it's called compar() rather than compare().
Historical reasons, I suppose.)
I suspect the famous 6 char external linkage name limit.

Sure, but parameter names don't have external linkage.
True, but humans are habitual creatures. The habit was to make all
function names in the Standard 6 chars or less; that this wasn't
necessary in the case of a function pointer function parameter did not
manage to break the habit.

Richard
Mar 5 '07 #8

Richard Bos wrote:
Keith Thompson <ks***@mib.orgwrote:
CBFalconer <cb********@yahoo.comwrites:
Keith Thompson wrote:
>>
>(Hmm, I wonder why it's called compar() rather than compare().
>Historical reasons, I suppose.)
>
I suspect the famous 6 char external linkage name limit.
Sure, but parameter names don't have external linkage.

True, but humans are habitual creatures. The habit was to make all
function names in the Standard 6 chars or less; [ ... ]
Since the Standard library is a part of the implementation, it need
not be constrained by the rules of the C standard, isn't it?

Mar 5 '07 #9
In article <45*****************@news.xs4all.nl>,
Richard Bos <rl*@hoekstra-uitgeverij.nlwrote:
>(Hmm, I wonder why it's called compar() rather than compare().
Historical reasons, I suppose.)
I suspect the famous 6 char external linkage name limit.
>Sure, but parameter names don't have external linkage.
>True, but humans are habitual creatures. The habit was to make all
function names in the Standard 6 chars or less; that this wasn't
necessary in the case of a function pointer function parameter did not
manage to break the habit.
Does the use of the name "compar" in the standard have any normative
effect at all? Surely implementations are not required to declare it
with that name (since no program can tell the difference)? It seems
to be a purely editorial choice.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 5 '07 #10
Richard Tobin said:

<snip>
Does the use of the name "compar" in the standard have any normative
effect at all?
No, none whatsoever.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 5 '07 #11
ri*****@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <45*****************@news.xs4all.nl>,
Richard Bos <rl*@hoekstra-uitgeverij.nlwrote:
(Hmm, I wonder why it's called compar() rather than compare().
Historical reasons, I suppose.)
I suspect the famous 6 char external linkage name limit.
Sure, but parameter names don't have external linkage.
True, but humans are habitual creatures. The habit was to make all
function names in the Standard 6 chars or less; that this wasn't
necessary in the case of a function pointer function parameter did not
manage to break the habit.

Does the use of the name "compar" in the standard have any normative
effect at all?
No.
It seems to be a purely editorial choice.
Yes, but one probably born from habit.

Richard
Mar 5 '07 #12
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>>(Hmm, I wonder why it's called compar() rather than compare().
Historical reasons, I suppose.)
>I suspect the famous 6 char external linkage name limit.
>Sure, but parameter names don't have external linkage.
I looked up qsort in successively older Unix manuals, and the answer
appears to be that it's called compar() because it *did* originally
have external linkage. In third edition Unix, the qsort() function
did not take an argument for the comparison function, it just called
compar(). A default version of compar() was provided in the library
which did a byte-by-byte comparison. At that time the functions were
written in assembler, but C almost certainly inherited the name.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 5 '07 #13
Richard Tobin wrote:
In article <45*****************@news.xs4all.nl>,
Richard Bos <rl*@hoekstra-uitgeverij.nlwrote:
(Hmm, I wonder why it's called compar() rather than compare().
Historical reasons, I suppose.)
I suspect the famous 6 char external linkage name limit.
Sure, but parameter names don't have external linkage.
True, but humans are habitual creatures. The habit was to make all
function names in the Standard 6 chars or less; that this wasn't
necessary in the case of a function pointer function parameter did not
manage to break the habit.

Does the use of the name "compar" in the standard have any normative
effect at all? Surely implementations are not required to declare it
with that name (since no program can tell the difference)? It seems
to be a purely editorial choice.
Programs can tell the difference by defining compar as a macro which
would cause a syntax error, before including the standard header.
Because of this (and the fact that compar is not a reserved name),
implementations are not even allowed to use compar as a parameter name.

Mar 5 '07 #14
su**************@yahoo.com, India wrote:
>
What is meant by stable qsort ?
/* BEGIN output from pt_sort.c */

Arrays of type char *,
are being sorted by string length.

This is the original order of the test array:
one
two
three
four
five
six
seven
eight
nine
ten

Stable insertionsort:
one
two
six
ten
four
five
nine
three
seven
eight

Nonstable simple selection sort;
four is after nine, and three is after eight:
one
two
six
ten
five
nine
four
eight
three
seven

Standard library qsort, unspecified ordering of equal keys:
six
two
one
ten
five
nine
four
eight
seven
three

/* END output from pt_sort.c */

/* BEGIN pt_sort.c */

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

#define SORT_FUNCTIONS { \
no_sort, "This is the original order of the test array:",\
i_sort, "Stable insertionsort:", \
slsort, "Nonstable simple selection sort;\nfour is " \
"after nine, and three is after eight:", \
qsort, "Standard library qsort, " \
"unspecified ordering of equal keys:" \
}
#define NUMBERS { \
"one", "two","three","four","five", \
"six","seven","eight","nine", "ten" \
}
#define NMEMB (sizeof numbers / sizeof *numbers)
#define SORTS (sizeof s_F / sizeof *s_F)

int lencomp(const void *, const void *);
void no_sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
void i_sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
void slsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));

int main(void)
{
size_t element, sort;
struct sf {
void (*function)(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
const char *description;
} s_F[] = SORT_FUNCTIONS;
const char *numbers[] = NUMBERS;
char *copy[NMEMB];

puts("/* BEGIN output from pt_sort.c */\n\nArrays of type "
"char *,\nare being sorted by string length.");
for (sort = 0; sort != SORTS; ++sort) {
putchar('\n');
puts(s_F[sort].description);
memcpy(copy, numbers, sizeof copy);
s_F[sort].function(copy, NMEMB, sizeof *copy, lencomp);
for (element = 0; element != NMEMB; ++element) {
puts(copy[element]);
}
}
puts("\n/* END output from pt_sort.c */");
return 0;
}

int lencomp(const void *a, const void *b)
{
const size_t a_len = strlen(*(const char **)a);
const size_t b_len = strlen(*(const char **)b);

return b_len a_len ? -1 : b_len != a_len;
}

void no_sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
base, nmemb, size, compar;
}

void i_sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
unsigned char *array, *high, *low, *p1, *p2, *end, swap;

if (nmemb-- 1) {
array = base;
do {
low = array;
array += size;
high = array;
while (compar(low, high) 0) {
p1 = low;
p2 = high;
end = p2 + size;
do {
swap = *p1;
*p1++ = *p2;
*p2++ = swap;
} while (p2 != end);
if (low == base) {
break;
}
high = low;
low -= size;
}
} while (--nmemb != 0);
}
}

void slsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
size_t tail;
unsigned char *array, *first, *middle, *p1, *p2, *end, swap;

for (array = base; nmemb-- 1; array += size) {
first = middle = array + size;
tail = nmemb;
while (--tail != 0) {
middle += size;
if (compar(first, middle) 0) {
first = middle;
}
}
if (compar(array, first) 0) {
p1 = array;
p2 = first;
end = p2 + size;
do {
swap = *p1;
*p1++ = *p2;
*p2++ = swap;
} while (p2 != end);
}
}
}

/* END pt_sort.c */
--
pete
Mar 11 '07 #15

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

Similar topics

8
2051
by: Joe | last post by:
Hi, The declaration of qsort is: void qsort(void *base, size_t count, size_t size, int (*comp)(const void *e1, const void *e2)); Since const is used in the declaration of the comparison...
5
3848
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.
13
2592
by: buda | last post by:
I had some spare time and decided to try to implement the standard library qsort. This is a pretty simpleminded attempt, but it seems to be working. I have to point out that efficiency is not at...
14
1320
by: streamkid | last post by:
i'm a learning newbie at c++... and i have the following question... reading some source code, i saw this: int function(const void * one, const void * two) { int var1, var2; var1 =...
30
2692
by: gaoxtwarrior | last post by:
a sort which is stable means it keeps the object's original order. A sort which is in place is stable. does anyone can explain me what is sort in place? thx.
4
1935
by: Szabolcs Nagy | last post by:
in the code below i thought the function call in g() could be easily optimized out so that g() becomes the same as h() (which becomes {return 0;}) executing 'gcc -O3 -S' i found that gcc does...
18
2610
by: mdh | last post by:
May I ask the following. By K&R's own admission, the example used to describe function pointers is complex ( on P119). In addition, the use of casts has been stated by some on this group as...
17
235
by: Ron Ford | last post by:
Is qsort an intrinsic for C? -- We must respect the other fellow's religion, but only in the sense and to the extent that we respect his theory that his wife is beautiful and his children smart....
61
5767
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
7144
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7427
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7085
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.