Got stuck with quicksort code from the "Unleashed C".

  • Thread starter grishin-mailing-lists
  • Start date
G

grishin-mailing-lists

Hello,

I'm trying to cope with quick sorting algorithm (I've been doing it
for a week at least).
My target is to write my own version.
I decided to learn && implement simplified algorithm first (that's why
Vol.3 of Knuth is not a proper source).
Now I'am absolutely confused because algorithms from Wirth's book,
McConnel book and wikipedia seem to be not working (or weird-working
at least) and I'm going to ask in a proper algorithms group.

In addition,
the code of simplified quick sort gives "stack overflow" too!

#include <stdio.h>

#define N (sizeof k / sizeof k[0])

void QSORTB(int * A, int l, int r)
{
int loc;

if (l < r) {
int i = l,
j = r;
int tmp,
pivot = A[l];
/* Divide piles into partitions */
for (;;) {
while ((A[j] >= pivot) && (j > l))
j--;
while ((A <= pivot) && (i < r))
i++;
if (i < j) {
tmp = A;
A = A[j];
A[j] = tmp;
} else {
loc = j;
break;
}
}
/* Recurse */
QSORTB(A, l, loc);
QSORTB(A, loc + 1, r);
}
}


int main (int argc, char **argv)
{
int k[] = { 503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154,
509, 612, 677, 765, 703 };
int i;

for (i = 0; i < N; i++)
printf("%d, ", k);
putchar('\n');

QSORTB(k, 0, N - 1);

for (i = 0; i < N; i++)
printf("%d, ", k);

return 0;
}

Thank you.
 
E

Eric Sosman

Hello,

I'm trying to cope with quick sorting algorithm (I've been doing it
for a week at least).
My target is to write my own version.
I decided to learn&& implement simplified algorithm first (that's why
Vol.3 of Knuth is not a proper source).
Now I'am absolutely confused because algorithms from Wirth's book,
McConnel book and wikipedia seem to be not working (or weird-working
at least) and I'm going to ask in a proper algorithms group.
[... code snipped ...]

This isn't a "proper algorithms group" at all, but a group
about the C programming language. One possible PAG might be
comp.programming. But anyhow ...

The missing piece (or a missing piece) in your version is
the failure to put the pivot element itself into the right
position after partitioning. You're just leaving it where you
found it, which happens to be at the leftmost position in the
array. Then the recursive call for that portion of the array
finds the pivot still at the left, followed by a solid block
of elements all <= the pivot -- so its partitioning work does
nothing at all. And then it calls a still deeper recursion
with exactly the same situation, the deeper recursion does
nothing and calls a still deeper recursion, ...

When you can't figure something out by inspection, a very
simple (even crude) technique is startlingly effective: Add a
few printf() calls at strategic spots. In this instance, a
fairly obvious spot is the start of each QSORTB invocation:
print out the values of l,r to indicate which part of the
array is being worked on -- You would have seen a progression
like [0,15], [0,7], [0,7], [0,7], [0,7], ..., crash, and you
would have figured out pretty quickly that the recursive calls
were making no progress. It wouldn't tell you *why* they were
making no progress, but it would have focused your attention.

Two further suggestions: In a recursive setting it's often
helpful to print the recursion depth along with the interesting
values, which would require that you change the code a little
to adjust the depth as you call and return (you can rip the
extra code out later). Second, in a situation where a crash
is expected or likely, it may be better to fprintf(stderr,...)
than to printf(), to avoid issues with buffering on stdout.
(For the topicality police: That last bit is, I claim, about C.)
 
B

Ben Bacarisse

I'm trying to cope with quick sorting algorithm (I've been doing it
for a week at least).
My target is to write my own version.

Is this coursework?
I decided to learn && implement simplified algorithm first (that's why
Vol.3 of Knuth is not a proper source).
Now I'am absolutely confused because algorithms from Wirth's book,
McConnel book and wikipedia seem to be not working (or weird-working
at least) and I'm going to ask in a proper algorithms group.

In addition,
the code of simplified quick sort gives "stack overflow" too!

#include <stdio.h>

#define N (sizeof k / sizeof k[0])

I'd prefer this to be a function-like macro:

#define SZ(a) (sizeof (a) / sizeof (a)[0])
void QSORTB(int * A, int l, int r)
{
int loc;

if (l < r) {
int i = l,
j = r;
int tmp,
pivot = A[l];

Strange layout. This discourages people from reading the code!
/* Divide piles into partitions */
for (;;) {
while ((A[j] >= pivot) && (j > l))
j--;
while ((A <= pivot) && (i < r))
i++;
if (i < j) {
tmp = A;
A = A[j];
A[j] = tmp;
} else {
loc = j;
break;
}
}
/* Recurse */
QSORTB(A, l, loc);
QSORTB(A, loc + 1, r);
}
}


The "shape" of the code is right but the details are wrong. Take a
simple example and work it through on paper (or in a debugger). Try
sorting { 11, 10 }. If you get stuck post again with what you think
happens in this case.

You get more answers in comp.programming since this is not really (at
least not yet) a C question.

<snip>
 
G

grishin-mailing-lists

Thanks for the replies, guys.

No, It's not a coursework, I've graduated 5 years ago. Just taking
some disciplines for myself (and on my own).
I thought that it's possible to get feedback from [one of] the
author of the book. 10 years have past since publication. I'm
unlikely the first who found the bug (IS it a bug or typographical
error or peculiarities of Russian edition?).
Well, I'm able to trace programs, BTW thank you for fprintf trick.
I've had few crashes before and noticed a weird printf behavior.
 
P

Phred Phungus

Thanks for the replies, guys.

No, It's not a coursework, I've graduated 5 years ago. Just taking
some disciplines for myself (and on my own).
I thought that it's possible to get feedback from [one of] the
author of the book. 10 years have past since publication. I'm
unlikely the first who found the bug (IS it a bug or typographical
error or peculiarities of Russian edition?).
Well, I'm able to trace programs, BTW thank you for fprintf trick.
I've had few crashes before and noticed a weird printf behavior.


Dann Corbit's around. I'm surprised that he doesn't have a quicksort
that reflects the changes he talks on p. 526.

$ ls -l
total 188
-rw-r--r-- 1 dan dan 34850 2000-02-24 15:46 ALLSORT.H
-rw-r--r-- 1 dan dan 10723 2000-02-24 15:47 BAR.C
-rw-r--r-- 1 dan dan 1064 2000-02-24 15:13 BARPROTO.H
-rw-r--r-- 1 dan dan 7731 2000-02-24 15:23 COCKUS.C
-rw-r--r-- 1 dan dan 1549 1999-12-03 07:29 COMPOBJ.H
-rw-r--r-- 1 dan dan 1642 1999-12-03 07:30 COMPSTR.H
-rw-r--r-- 1 dan dan 1313 1999-12-03 07:29 CPPCOMP.H
-rw-r--r-- 1 dan dan 2243 2000-02-24 15:55 DCOMP.H
-rw-r--r-- 1 dan dan 4577 2000-01-04 15:04 DCSRLIC.TXT
-rw-r--r-- 1 dan dan 13194 2000-02-24 15:20 DISTRIBS.C
-rw-r--r-- 1 dan dan 1702 2000-02-24 15:21 DISTRIBS.H
-rw-r--r-- 1 dan dan 577 1999-12-03 07:29 DOUBLE.C
-rw-r--r-- 1 dan dan 37897 2000-02-24 15:47 GENPROTO.H
-rw-r--r-- 1 dan dan 602 2000-02-24 15:47 INTELTYP.H
-rw-r--r-- 1 dan dan 2287 1999-12-03 07:29 KEYXFRM.C
-rw-r--r-- 1 dan dan 352 1999-12-03 07:30 MTRAND.H
-rw-r--r-- 1 dan dan 1976 2000-02-24 15:46 SICOMP.H
-rw-r--r-- 1 dan dan 200 1999-12-03 07:31 SINT.C
-rw-r--r-- 1 dan dan 2257 2000-02-24 15:47 STRCOMP.H
-rw-r--r-- 1 dan dan 579 1999-12-03 07:29 STRINGS.C
-rw-r--r-- 1 dan dan 11992 2000-02-24 15:47 TEST.C
$

[could only paste as a quote: what a pain]

Maybe he could also talk about these programs a bit.
 
D

Dann Corbit

@q21g2000yqm.googlegroups.com>, grishin-mailing-
(e-mail address removed) says...
Hello,

I'm trying to cope with quick sorting algorithm (I've been doing it
for a week at least).
My target is to write my own version.
I decided to learn && implement simplified algorithm first (that's why
Vol.3 of Knuth is not a proper source).
Now I'am absolutely confused because algorithms from Wirth's book,
McConnel book and wikipedia seem to be not working (or weird-working
at least) and I'm going to ask in a proper algorithms group.

In addition,
the code of simplified quick sort gives "stack overflow" too!

#include <stdio.h>

#define N (sizeof k / sizeof k[0])

void QSORTB(int * A, int l, int r)
{
int loc;

if (l < r) {
int i = l,
j = r;
int tmp,
pivot = A[l];
/* Divide piles into partitions */
for (;;) {
while ((A[j] >= pivot) && (j > l))
j--;
while ((A <= pivot) && (i < r))
i++;
if (i < j) {
tmp = A;
A = A[j];
A[j] = tmp;
} else {
loc = j;
break;
}
}
/* Recurse */
QSORTB(A, l, loc);
QSORTB(A, loc + 1, r);
}
}


To use the code from the book, try this (you will also need to link in
the object from cockus.c which contains the prng):

#include <stdio.h>

#define E_TYPE_SIGNED_INT
#include "allsort.h"

int main (int argc, char **argv)
{
int k[] = { 503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154,
509, 612, 677, 765, 703
};
int i;
size_t N = sizeof k / sizeof k[0];

for (i = 0; i < N; i++)
printf("%d, ", k);
putchar('\n');

QSORTB(k, 0, N - 1);

for (i = 0; i < N; i++)
printf("%d, ", k);

return 0;
}


/* Output:
503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765,
703,
61, 87, 154, 170, 275, 426, 503, 509, 512, 612, 653, 677, 703, 765, 897,
908,
*/

P.S.
Consider the note:
/*
Primitive quicksort. This is a bad algorithm for general purposes.
Don't use it. Use IQSORT5 instead. This thing is a bomb waiting to go
off.
*/

Further:

None of the improvements discussed in the text are included in this
primitive version, which is intended only to demonstrate the fundamental
concept of the unadorned algorithm.

The design of the book's algorithms for sorting is something like a code
generation. The idea is to approach the C++ template metaphor in C, so
that one fundamental set of algorithms can be used for any data type.

The macro:
#define E_TYPE_SIGNED_INT

generates a sort system for signed integer.
 
P

Phred Phungus

Dann said:
To use the code from the book, try this (you will also need to link in
the object from cockus.c which contains the prng):

[lots of code, big snips]

I can't get COCKUS.C to compile, Dann:

$ gcc -D_GNU_SOURCE -Wall -Wextra -c COCKUS.C -o ****.o
gcc: error trying to exec 'cc1plus': execvp: Permission denied
$ ls -l | grep ****
-rw-r--r-- 1 dan dan 7789 2010-03-09 01:06 COCKUS.C
-rw-r--r-- 1 dan dan 7731 2000-02-24 15:23 COCKUS.C~
$ gcc -D_GNU_SOURCE -Wall -Wextra a1.c -o out
$ ./out
BAR.C
COCKUS.C
COCKUS.C~
CPPCOMP.H
DCOMP.H
DCSRLIC.TXT
DISTRIBS.C
DOUBLE.C
KEYXFRM.C
SICOMP.H
SINT.C
STRCOMP.H
STRINGS.C
TEST.C
status is 0

So this is all happening in the same directory, which is a good thing
when you're talking standard C. I get gcc to compile a1.c, which is a
linux utility written in C to show files that match \\.C in this case.

BTW, the poor guy's name is Cokus, so this is the worst of mistyping
when you're dealing with this name. The extra c turns the long O into a
short o, and you've made this fellow's name a fallic nightmare.

$ cat COCKUS.C
/*
** This is the home page of the Mersenne Twister PRNG:
** http://www.math.keio.ac.jp/~matumoto/emt.html
**
** This is the ``Mersenne Twister'' random number generator MT19937, which
** generates pseudorandom integers uniformly distributed in 0..(2^32 - 1)
** starting from any odd seed in 0..(2^32 - 1). This version is a recode
** by Shawn Cokus ([email protected]) on March 8, 1998 of a
version by
** Takuji Nishimura (who had suggestions from Topher Cooper and Marc
Rieffel in
** July-August 1997).
**
** Effectiveness of the recoding (on Goedel2.math.washington.edu, a DEC
Alpha
** running OSF/1) using GCC -O3 as a compiler: before recoding: 51.6 sec. to
** generate 300 million random numbers; after recoding: 24.0 sec. for
the same
** (i.e., 46.5% of original time), so speed is now about 12.5 million random
** number generations per second on this machine.
**
** According to the URL <http://www.math.keio.ac.jp/~matumoto/emt.html>
** (and paraphrasing a bit in places), the Mersenne Twister is ``designed
** with consideration of the flaws of various existing generators,'' has
** a period of 2^19937 - 1, gives a sequence that is 623-dimensionally
** equidistributed, and ``has passed many stringent tests, including the
** die-hard test of G. Marsaglia and the load test of P. Hellekalek and
** S. Wegenkittl.'' It is efficient in memory usage (typically using 2506
** to 5012 bytes of static data, depending on data type sizes, and the code
** is quite short as well). It generates random numbers in batches of 624
** at a time, so the caching and pipelining of modern systems is exploited.
** It is also divide- and mod-free.
**
** This library is free software; you can redistribute it and/or modify it
** under the terms of the GNU Library General Public License as published by
** the Free Software Foundation (either version 2 of the License or, at your
** option, any later version). This library 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 Library General Public License for more details. You should have
** received a copy of the GNU Library General Public License along with this
** library; if not, write to the Free Software Foundation, Inc., 59 Temple
** Place, Suite 330, Boston, MA 02111-1307, USA.
**
** The code as Shawn received it included the following notice:
**
** Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura. When
** you use this, send an e-mail to <[email protected]> with
** an appropriate reference to your work.
**
** It would be nice to CC: <[email protected]> when you write.
**
*/

#include <stdio.h>
#include <stdlib.h>
#include "mtrand.h"
/*
uint32 must be an unsigned integer type capable of holding at least 32
bits; exactly 32 should be fastest, but 64 is better on an Alpha with
GCC at -O3 optimization so try your options and see what's best for you
*/

typedef unsigned long uint32;

/* length of state vector */
#define N (624)

/* a period parameter */
#define M (397)

/* a magic constant */
#define K (0x9908B0DFU)

/* mask all but highest bit of u */
#define hiBit(u) ((u) & 0x80000000U)

/* mask all but lowest bit of u */
#define loBit(u) ((u) & 0x00000001U)

/* mask the highest bit of u */
#define loBits(u) ((u) & 0x7FFFFFFFU)

/* move hi bit of u to hi bit of v */
#define mixBits(u, v) (hiBit(u)|loBits(v))

/* state vector + 1 extra to not violate ANSI C */
static uint32 state[N + 1];

/* next random value is computed from here */
static uint32 *next;

/* can *next++ this many times before reloading */
static int left = -1;


/*
**
** We initialize state[0..(N-1)] via the generator
**
** x_new = (69069 * x_old) mod 2^32
**
** from Line 15 of Table 1, p. 106, Sec. 3.3.4 of Knuth's
** _The Art of Computer Programming_, Volume 2, 3rd ed.
**
** Notes (SJC): I do not know what the initial state requirements
** of the Mersenne Twister are, but it seems this seeding generator
** could be better. It achieves the maximum period for its modulus
** (2^30) iff x_initial is odd (p. 20-21, Sec. 3.2.1.2, Knuth); if
** x_initial can be even, you have sequences like 0, 0, 0, ...;
** 2^31, 2^31, 2^31, ...; 2^30, 2^30, 2^30, ...; 2^29, 2^29 + 2^31,
** 2^29, 2^29 + 2^31, ..., etc. so I force seed to be odd below.
**
** Even if x_initial is odd, if x_initial is 1 mod 4 then
**
** the lowest bit of x is always 1,
** the next-to-lowest bit of x is always 0,
** the 2nd-from-lowest bit of x alternates ... 0 1 0 1 0 1 0 1 ... ,
** the 3rd-from-lowest bit of x 4-cycles ... 0 1 1 0 0 1 1 0 ... ,
** the 4th-from-lowest bit of x has the 8-cycle ... 0 0 0 1 1 1 1 0 ... ,
** ...
**
** and if x_initial is 3 mod 4 then
**
** the lowest bit of x is always 1,
** the next-to-lowest bit of x is always 1,
** the 2nd-from-lowest bit of x alternates ... 0 1 0 1 0 1 0 1 ... ,
** the 3rd-from-lowest bit of x 4-cycles ... 0 0 1 1 0 0 1 1 ... ,
** the 4th-from-lowest bit of x has the 8-cycle ... 0 0 1 1 1 1 0 0 ... ,
** ...
**
** The generator's potency (min. s>=0 with (69069-1)^s = 0 mod 2^32) is
** 16, which seems to be alright by p. 25, Sec. 3.2.1.3 of Knuth. It
** also does well in the dimension 2..5 spectral tests, but it could be
** better in dimension 6 (Line 15, Table 1, p. 106, Sec. 3.3.4, Knuth).
**
** Note that the random number user does not see the values generated
** here directly since reloadMT() will always munge them first, so maybe
** none of all of this matters. In fact, the seed values made here could
** even be extra-special desirable if the Mersenne Twister theory says
** so-- that's why the only change I made is to restrict to odd seeds.
*/

void mtsrand(uint32 seed)
{
register uint32 x = (seed | 1U) & 0xFFFFFFFFU,
*s = state;
register int j;

for (left = 0, *s++ = x, j = N; --j;
*s++ = (x *= 69069U) & 0xFFFFFFFFU);
}


uint32
reloadMT(void)
{
register uint32 *p0 = state,
*p2 = state + 2,
*pM = state + M,
s0,
s1;
register int j;

if (left < -1)
mtsrand(4357U);

left = N - 1, next = state + 1;

for (s0 = state[0], s1 = state[1], j = N - M + 1; --j; s0 = s1, s1
= *p2++)
*p0++ = *pM++ ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U);

for (pM = state, j = M; --j; s0 = s1, s1 = *p2++)
*p0++ = *pM++ ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U);

s1 = state[0], *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K
: 0U);
s1 ^= (s1 >> 11);
s1 ^= (s1 << 7) & 0x9D2C5680U;
s1 ^= (s1 << 15) & 0xEFC60000U;
return (s1 ^ (s1 >> 18));
}


uint32 mtrand(void)
{
uint32 y;

if (--left < 0)
return (reloadMT());

y = *next++;
y ^= (y >> 11);
y ^= (y << 7) & 0x9D2C5680U;
y ^= (y << 15) & 0xEFC60000U;
return (y ^ (y >> 18));
}

#ifdef UNIT_TEST
int main(void)
{
int j;

/* you can seed with any uint32, but the best are odds in 0..(2^32
- 1) */
mtsrand(4357U);

/* print the first 2,002 random numbers seven to a line as an
example */
for (j = 0; j < 2002; j++)
printf(" %10lu%s", (unsigned long) mtrand(), (j % 7) == 6 ?
"\n" : "");

return (EXIT_SUCCESS);
}
#endif

// gcc -D_GNU_SOURCE -Wall -Wextra -c COCKUS.C -o ****.o
$

BTW, if Peter Seebach would look through this thread, he would see
evidence of me reading C reference on a night in which his crystal ball
erred on the side of sounding like my mormon ex-wife.
 
D

Dann Corbit

Dann said:
To use the code from the book, try this (you will also need to link in
the object from cockus.c which contains the prng):

[lots of code, big snips]

I can't get COCKUS.C to compile, Dann:

$ gcc -D_GNU_SOURCE -Wall -Wextra -c COCKUS.C -o ****.o
gcc: error trying to exec 'cc1plus': execvp: Permission denied

No problems for me.
dcorbit@DCORBIT2008 /c/dannfast/e_drive/book/ncode
$ gcc -Wall -ansi -pedantic -c COCKUS.C -o cockus.o

I don't know why you do not have permission to execute the preprocessor.
 
S

Seebs

I can't get COCKUS.C to compile, Dann:
$ gcc -D_GNU_SOURCE -Wall -Wextra -c COCKUS.C -o ****.o
gcc: error trying to exec 'cc1plus': execvp: Permission denied
$ ls -l | grep ****
-rw-r--r-- 1 dan dan 7789 2010-03-09 01:06 COCKUS.C
-rw-r--r-- 1 dan dan 7731 2000-02-24 15:23 COCKUS.C~
$ gcc -D_GNU_SOURCE -Wall -Wextra a1.c -o out
$ ./out

Your problem is that, unbeknownst to you, UNIX is being more case
sensitive than you expect. In particular, gcc assumes that a file with
a ".C" extension is C++, not C, and for some reason, your system's C++
compiler is busted.
BTW, if Peter Seebach would look through this thread, he would see
evidence of me reading C reference on a night in which his crystal ball
erred on the side of sounding like my mormon ex-wife.

Yeah, but also evidence of you being characteristically unwilling to think
about what you see and draw conclusions from it. "cc1plus" sure sounds
like there's a "plus" somewhere in it, to me, and UNIX systems are typically
case-sensitive, so naming something in all caps, plus getting unexpected
behavior, suggests to me that trying it in lowercase might help. For
that matter:

$ man gcc
For any given input file, the file name suffix determines what kind of
compilation is done:
....
file.C
C++ source code which must be preprocessed. Note that in .cxx, the
last two letters must both be literally x. Likewise, .C refers to
a literal capital C.

All you have to do to find this is search for ".C" in the gcc man page.
This is not all that hard.

-s
 
P

Phred Phungus

Dann said:
The macro:
#define E_TYPE_SIGNED_INT

generates a sort system for signed integer.

No, it doesn't.

#if defined(ETYPE_UNSIGNED_INT)
#include "uicomp.h"
#elif defined(ETYPE_SIGNED_INT)
#include "sicomp.h"
#elif defined(ETYPE_UNSIGNED_LONG)
#include "ulcomp.h"
#elif defined(ETYPE_SIGNED_LONG)
#include "slcomp.h"
....

You've got an extra underscore there. But that's small beans.

First of all, you misattributed, sullying the good Cokus name.
Secondly, you managed to make the whole directory in caps, which buggers
the include statements to say nothing of making my compiler believe
that it was C++.

I'm rather proud of the solution I found for this:

for f in *; do mv $f `echo $f | tr '[:upper:]' '[:lower:]'`; done

And I figure that since you got thirteen cents from me buying your book,
I think I am not inappropriate to expect this software to work. I paid
good money for it.

Right now, allsort.h isn't looking too hot:

$ gcc -D_GNU_SOURCE -Wall -Wextra cok.o d1.c -o out
In file included from d1.c:4:
allsort.h:122: warning: unused parameter ‘array’
allsort.h:132: warning: unused parameter ‘array’
allsort.h: In function ‘RadixMsd_si’:
allsort.h:946: warning: comparison between signed and unsigned
allsort.h: In function ‘RadixLsd_si’:
allsort.h:1008: warning: comparison between signed and unsigned
allsort.h:1017: warning: missing braces around initializer
allsort.h:1017: warning: (near initialization for ‘cnts[0]’)
allsort.h:1027: warning: comparison between signed and unsigned
allsort.h:1035: warning: unused variable ‘count’
allsort.h: At top level:
allsort.h:1001: warning: unused parameter ‘keysize’
d1.c: In function ‘main’:
d1.c:14: warning: comparison between signed and unsigned
d1.c:20: warning: comparison between signed and unsigned
$ ./out
503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765,
703,
61, 87, 154, 170, 275, 426, 503, 509, 512, 612, 653, 677, 703, 765, 897,
908, $

So, this looks like it's basically behaving, but what about all these
warnings?
 
D

Dann Corbit

No, it doesn't.

#if defined(ETYPE_UNSIGNED_INT)
#include "uicomp.h"
#elif defined(ETYPE_SIGNED_INT)
#include "sicomp.h"
#elif defined(ETYPE_UNSIGNED_LONG)
#include "ulcomp.h"
#elif defined(ETYPE_SIGNED_LONG)
#include "slcomp.h"
...

You've got an extra underscore there. But that's small beans.

No, you don't have the latest code from the book.
First of all, you misattributed, sullying the good Cokus name.
Secondly, you managed to make the whole directory in caps, which buggers
the include statements to say nothing of making my compiler believe
that it was C++.

It's not an attribution, it's a file name. It was the name originally
attached to the file (I did not name it).
I'm rather proud of the solution I found for this:

for f in *; do mv $f `echo $f | tr '[:upper:]' '[:lower:]'`; done

And I figure that since you got thirteen cents from me buying your book,
I think I am not inappropriate to expect this software to work. I paid
good money for it.

I get no commission or royalties.
Right now, allsort.h isn't looking too hot:

$ gcc -D_GNU_SOURCE -Wall -Wextra cok.o d1.c -o out
In file included from d1.c:4:
allsort.h:122: warning: unused parameter ?array?
allsort.h:132: warning: unused parameter ?array?
allsort.h: In function ?RadixMsd_si?:
allsort.h:946: warning: comparison between signed and unsigned
allsort.h: In function ?RadixLsd_si?:
allsort.h:1008: warning: comparison between signed and unsigned
allsort.h:1017: warning: missing braces around initializer
allsort.h:1017: warning: (near initialization for ?cnts[0]?)
allsort.h:1027: warning: comparison between signed and unsigned
allsort.h:1035: warning: unused variable ?count?
allsort.h: At top level:

I'm sure you can work it out well enough.
Splint will give you even more things to examine.
 
A

al

I'm rather proud of the solution I found for this:

for f in *; do mv $f `echo $f | tr '[:upper:]' '[:lower:]'`; done

And proud you should be.

At last you have demonstrated the ability to use Google to find answers
to very simple questions. Keep up the good work.
 
P

Phred Phungus

al said:
I'm rather proud of the solution I found for this:

for f in *; do mv $f `echo $f | tr '[:upper:]' '[:lower:]'`; done

And proud you should be.

At last you have demonstrated the ability to use Google to find answers
to very simple questions. Keep up the good work.

So you don't think I read about regex's for an hour before I knew what
to look for?

Do you, al, have a quicksort to share?
 
R

Richard Bos

Dann Corbit said:
No problems for me.
dcorbit@DCORBIT2008 /c/dannfast/e_drive/book/ncode
$ gcc -Wall -ansi -pedantic -c COCKUS.C -o cockus.o

I don't know why you do not have permission to execute the preprocessor.

Well, if you were managing his system, would _you_ give him that
permission?

Richard
 
P

Phred Phungus

Richard said:
Dann Corbit said:
Well, if you were managing his system, would _you_ give him that
permission?

Richard

Ouch. Well Richard's probably right on that. This is my 6th and
longest-lasting linux install. That includes 2 Solarises and 4 Ubuntus.

I do find unusual ways to kill my OS.
 
G

grishin-mailing-lists

@q21g2000yqm.googlegroups.com>, grishin-mailing-
(e-mail address removed) says...




I'm trying to cope with quick sorting algorithm (I've been doing it
for a week at least).
My target is to write my own version.
I decided to learn && implement simplified algorithm first (that's why
Vol.3 of Knuth is not a proper source).
Now I'am absolutely confused because algorithms from Wirth's book,
McConnel book and wikipedia seem to be not working (or weird-working
at least) and I'm going to ask in a proper algorithms group.
In addition,
the code of simplified quick sort gives "stack overflow" too!
#include <stdio.h>
#define N (sizeof k / sizeof k[0])
void QSORTB(int * A, int l, int r)
{
int loc;
if (l < r) {
int i = l,
j = r;
int tmp,
pivot = A[l];
/* Divide piles into partitions */
for (;;) {
while ((A[j] >= pivot) && (j > l))
j--;
while ((A <= pivot) && (i < r))
i++;
if (i < j) {
tmp = A;
A = A[j];
A[j] = tmp;
} else {
loc = j;
break;
}
}
/* Recurse */
QSORTB(A, l, loc);
QSORTB(A, loc + 1, r);
}
}


To use the code from the book, try this (you will also need to link in
the object from cockus.c which contains the prng):

#include <stdio.h>

#define E_TYPE_SIGNED_INT
#include "allsort.h"

int main (int argc, char **argv)
{
int k[] = { 503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154,
509, 612, 677, 765, 703
};
int i;
size_t N = sizeof k / sizeof k[0];

for (i = 0; i < N; i++)
printf("%d, ", k);
putchar('\n');

QSORTB(k, 0, N - 1);

for (i = 0; i < N; i++)
printf("%d, ", k);

return 0;

}

/* Output:
503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765,
703,
61, 87, 154, 170, 275, 426, 503, 509, 512, 612, 653, 677, 703, 765, 897,
908,
*/

P.S.
Consider the note:
/*
Primitive quicksort. This is a bad algorithm for general purposes.
Don't use it. Use IQSORT5 instead. This thing is a bomb waiting to go
off.
*/

Further:

None of the improvements discussed in the text are included in this
primitive version, which is intended only to demonstrate the fundamental
concept of the unadorned algorithm.

The design of the book's algorithms for sorting is something like a code
generation. The idea is to approach the C++ template metaphor in C, so
that one fundamental set of algorithms can be used for any data type.

The macro:
#define E_TYPE_SIGNED_INT

generates a sort system for signed integer.


Thank you very much.
Everything works well.
 
D

Dann Corbit

@c16g2000yqd.googlegroups.com>, grishin-mailing-
(e-mail address removed) says...
Thank you very much.
Everything works well.

Glad that you got things straightened out.
If you have any further questions, I will be glad to give you a hand.
 
P

Phred Phungus

Seebs said:
1. That's not regexes at all.
2. The VERY FIRST google result for "convert filename to lowercase":
http://www.linuxjournal.com/content/convert-filenames-lowercase

gives a comparable (for most purposes) example with a detailed explanation.

If it takes an hour to read the VERY FIRST search result on the most
obvious search string...

-s

Seebs: the guy who knows what you're doing. I was reading about regex's
in favorable light: sunshine on a book.

Just fucking google off.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top