OT: radixsort

N

newbie

Hi,
could somebody explain me this code?:

void radixsort(unsigned int *array, unsigned int size) {
int x, y, z;
unsigned int *buckets[NUM_BUCKETS];
unsigned int count[NUM_BUCKETS];
unsigned int mask;

for(x = 0; x < NUM_BUCKETS; x++) {
buckets[x] = (unsigned int *) malloc(size * sizeof(unsigned int));
}

for(mask = 1; mask; mask = mask << 1) {
for(x = 0; x < NUM_BUCKETS; count[x++] = 0);

for(x = 0; x < size; x++) {
if(array[x] & mask) {
z = 1;
} else {
z = 0;
}
buckets[z][count[z]++] = array[x];
}

z = 0;
for(x = 0; x < NUM_BUCKETS; x++) {
for(y = 0; y < count[x]; y++) {
array[z++] = buckets[x][y];
}
}
}

for(x = 0; x < NUM_BUCKETS; x++) {
free(buckets[x]);
}
}


Thanks.
 
J

Jack Klein

Hi,
could somebody explain me this code?:

void radixsort(unsigned int *array, unsigned int size) {
int x, y, z;
unsigned int *buckets[NUM_BUCKETS];
unsigned int count[NUM_BUCKETS];
unsigned int mask;

for(x = 0; x < NUM_BUCKETS; x++) {
buckets[x] = (unsigned int *) malloc(size * sizeof(unsigned int));

The line above is poorly written for C, although it is legal unless it
happens to obscure the required diagnostic if there is no valid
prototype for malloc() in scope. If there is no prototype for
malloc() in scope, the behavior is undefined.
}

for(mask = 1; mask; mask = mask << 1) {
for(x = 0; x < NUM_BUCKETS; count[x++] = 0);

Some programmers never seem to grasp the fact that white space in C
source code does not increase either the size or execution time of the
final program.

Putting a semicolon on the end of a for loop like this is a fairly
common newbie mistake. Anyone who did that in any professional
organization with reasonable standards would be corrected.
for(x = 0; x < size; x++) {
if(array[x] & mask) {
z = 1;
} else {
z = 0;
}
buckets[z][count[z]++] = array[x];
}

z = 0;
for(x = 0; x < NUM_BUCKETS; x++) {
for(y = 0; y < count[x]; y++) {
array[z++] = buckets[x][y];
}
}
}

for(x = 0; x < NUM_BUCKETS; x++) {
free(buckets[x]);
}
}


Thanks.

It's hard to say for sure, without knowing the definition of the macro
NUM_BUCKETS, but it is quite possibly the source code for a function
that performs a radix sort on an array of unsigned integers.

If your question is about what a radix sort is and how it is used, try
Google.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jack said:
Hi,
could somebody explain me this code?:

void radixsort(unsigned int *array, unsigned int size) {
int x, y, z;
unsigned int *buckets[NUM_BUCKETS];
unsigned int count[NUM_BUCKETS];
unsigned int mask;

for(x = 0; x < NUM_BUCKETS; x++) {
buckets[x] = (unsigned int *) malloc(size * sizeof(unsigned int));


The line above is poorly written for C, although it is legal unless it
happens to obscure the required diagnostic if there is no valid
prototype for malloc() in scope. If there is no prototype for
malloc() in scope, the behavior is undefined.

}

for(mask = 1; mask; mask = mask << 1) {
for(x = 0; x < NUM_BUCKETS; count[x++] = 0);


Some programmers never seem to grasp the fact that white space in C
source code does not increase either the size or execution time of the
final program.

Putting a semicolon on the end of a for loop like this is a fairly
common newbie mistake. Anyone who did that in any professional
organization with reasonable standards would be corrected.

Jack, if I read the code correctly, the semicolon is properly placed, and is not
an error or a violation of coding standards (unless your coding standards are
needlessly strict).

Consider the effect of
for(x = 0; x < NUM_BUCKETS; count[x++] = 0);

The re-initialization expression
count[x++] = 0
initializes count[x] to zero, and increments x

To me, this for() statement looks like a standard 'initialize the array to zero'
loop. It could also be expressed as
for (x = 0; x < NUM_BUCKETS; x++) count[x] = 0;

[snip]


- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFCY6mVagVFX4UWr64RAi3eAJ9cBlhXxKE1uq3CFXWv4ZzLSfKAKwCdF5az
9wCZHmA3endJzXWB9pmqp5g=
=jJQO
-----END PGP SIGNATURE-----
 
P

pete

Lew said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jack said:
Hi,
could somebody explain me this code?:

void radixsort(unsigned int *array, unsigned int size) {
int x, y, z;
unsigned int *buckets[NUM_BUCKETS];
unsigned int count[NUM_BUCKETS];
unsigned int mask;

for(x = 0; x < NUM_BUCKETS; x++) {
buckets[x] = (unsigned int *) malloc(size * sizeof(unsigned int));


The line above is poorly written for C, although it is legal unless it
happens to obscure the required diagnostic if there is no valid
prototype for malloc() in scope. If there is no prototype for
malloc() in scope, the behavior is undefined.

}

for(mask = 1; mask; mask = mask << 1) {
for(x = 0; x < NUM_BUCKETS; count[x++] = 0);


Some programmers never seem to grasp the fact that white space in C
source code does not increase either
the size or execution time of the final program.

Putting a semicolon on the end of a for loop like this is a fairly
common newbie mistake. Anyone who did that in any professional
organization with reasonable standards would be corrected.

Jack, if I read the code correctly,
the semicolon is properly placed, and is not
an error or a violation of coding standards
(unless your coding standards are needlessly strict).

Consider the effect of
for(x = 0; x < NUM_BUCKETS; count[x++] = 0);

The re-initialization expression
count[x++] = 0
initializes count[x] to zero, and increments x

To me, this for() statement looks like a standard
'initialize the array to zero'
loop. It could also be expressed as
for (x = 0; x < NUM_BUCKETS; x++) count[x] = 0;


Saving typeing is a valid reason for writing code a certain way,
but it's not a strong reason. I generally like compound
statements for use with loops, ifs, and elses.

for(mask = 1; mask; mask = mask << 1) {
for(x = 0; x < NUM_BUCKETS; count[x++] = 0) {
;
}
}

for(mask = 1; mask; mask = mask << 1) {
for (x = 0; x < NUM_BUCKETS; x++) {
count[x] = 0;
}
}
 
N

Null_Ptr_72

Dear Newbie,

The only advice i have for you in regards to understanding this "loop"
loop is this:

1- Read from right to left.
2- Evaluate to true && || false. (in every-line)
3- Understand What is within the local stack ("scope").

Here is a place i suggest you visit:
http://www.math.wustl.edu/~ben/prog.html
 
K

Keith Thompson

Null_Ptr_72 said:
The only advice i have for you in regards to understanding this "loop"
loop is this:

1- Read from right to left.
2- Evaluate to true && || false. (in every-line)
3- Understand What is within the local stack ("scope").

I have no idea what your point 2 means. Is "&& ||" short for the
English "and/or"? If so, please spell it out. (Even given that
assumption, I don't know what you mean.)
 
J

Jack Klein

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jack said:
Hi,
could somebody explain me this code?:

void radixsort(unsigned int *array, unsigned int size) {
int x, y, z;
unsigned int *buckets[NUM_BUCKETS];
unsigned int count[NUM_BUCKETS];
unsigned int mask;

for(x = 0; x < NUM_BUCKETS; x++) {
buckets[x] = (unsigned int *) malloc(size * sizeof(unsigned int));


The line above is poorly written for C, although it is legal unless it
happens to obscure the required diagnostic if there is no valid
prototype for malloc() in scope. If there is no prototype for
malloc() in scope, the behavior is undefined.

}

for(mask = 1; mask; mask = mask << 1) {
for(x = 0; x < NUM_BUCKETS; count[x++] = 0);


Some programmers never seem to grasp the fact that white space in C
source code does not increase either the size or execution time of the
final program.

Putting a semicolon on the end of a for loop like this is a fairly
common newbie mistake. Anyone who did that in any professional
organization with reasonable standards would be corrected.

Jack, if I read the code correctly, the semicolon is properly placed, and is not
an error or a violation of coding standards (unless your coding standards are
needlessly strict).

Consider the effect of
for(x = 0; x < NUM_BUCKETS; count[x++] = 0);

The re-initialization expression
count[x++] = 0
initializes count[x] to zero, and increments x

I completely understood the function of the code, and indeed it could
have been replaced with a call to memset() to zero the array, now that
the C standard has finally caught up with reality and required all
bits 0 to be a valid representation of 0 for all integer types.

What I was specifically referring to was the placement of the
semicolon. Often such placement is a common newbie mistake. In this
case it does indeed make logical sense given that the loop needs no
body, and given the simple nature of the code this is relatively easy
to see.

But what if the action of the loop was somewhat more complex, and you
were performing a code inspection of the code? What if it were not
immediately obvious that the loop did not need a body, and the
following statement looked at first glance like it might be the body
of the loop? How much time should a reviewer waste figuring out
whether the semicolon is a mistake, and a potentially serious defect,
or not?

Coding standards, as opposed to "style guides", are about techniques
that statistical evidence proves lead to code that is easier to
inspect, has fewer defects, and is easier to maintain.

One of the MISRA C guidelines that I agree with, and there are quite a
few that I don't, is that a null statement must stand alone on a line
by itself.

Another which might or might not be MISRA but I can't remember
offhand, is that all selection statements have their controlled code
enclosed in braces, even if it is a single statement, even a null
statement. There is plenty of statistical evidence that this will
eliminate two or three defects per 100K lined of code, if not when
written then when modified or maintained.

Under the coding guidelines of my organization, which I have a large
part in defining and enforcing, this loop would be written as:

for(x = 0; x < NUM_BUCKETS; count[x++] = 0)
{
/* comment here is optional
;
}

There is no doubt whatsoever that this is an intentionally empty body.
To me, this for() statement looks like a standard 'initialize the array to zero'
loop. It could also be expressed as
for (x = 0; x < NUM_BUCKETS; x++) count[x] = 0;

[snip]

Once upon a time source code was edited on teletypes and stored on
punch cards, paper tape, magnetic tape, floppy disks, etc. Those days
are long gone. Try to find a hard disk drive under 40 gig, a display
terminal that displays less than 50 lines by a 100 or more columns.

The lack of white space is far more expensive than its presence.
 
C

Christopher Benson-Manica

Jack Klein <[email protected]> spoke thus:

My humble opinions follow...
But what if the action of the loop was somewhat more complex, and you
were performing a code inspection of the code? What if it were not
immediately obvious that the loop did not need a body, and the
following statement looked at first glance like it might be the body
of the loop?

First, I think that if the action of the loop were more complex, a
good programmer would simply dispense with the null statement
altogether. Second, the following statement would not, to me at
least, appear to be the body of the loop unless it were improperly
indented.
Under the coding guidelines of my organization, which I have a large
part in defining and enforcing, this loop would be written as:
for(x = 0; x < NUM_BUCKETS; count[x++] = 0)
{
/* comment here is optional
;
}

As long as one is going to use the braces and put the null statement
on a separate line, one may as well just write the loop
conventionally:

for( x=0; x < NUM_BUCKETS; x++ ) {
count[x]=0;
}

(my religion is a bit different than yours...)
The lack of white space is far more expensive than its presence.

College computing classes seem most stricken with the lack of
whitespace; style is one thing I didn't learn completely until I
entered the workforce.
 
N

Null_Ptr_72

1- Read from right to left.
2- Evaluate to true && || false. (in every-line)
3- Understand What is within the local stack ("scope").

I have no idea what your point 2 means. Is "&& ||" short for the
English "and/or"? If so, please spell it out. (Even given that
assumption, I don't know what you mean.)
Keith said:
I have no idea what your point 2 means. Is "&& ||" short for the
English "and/or"? If so, please spell it out. (Even given that
assumption, I don't know what you mean.)
Dear Keith,

It's just a pointer, Logical one"so-to-speak". The best way "for
me" to understand any program is to evaluate everyline as to whether or
not it will execute (&& || and/or) under what conditions (True && ||
False) will it execute. if you still want me to draw you a picture, i
will. And besides (Are you here to help and be helped? or scare away a
week old forum members?) Common man!
San Diego Supercomputer Center <*>
We must do something. This is something. Therefore, we must do
this.
 
M

Mark McIntyre

On 19 Apr 2005 08:44:11 -0700, in comp.lang.c , "Null_Ptr_72"

this was written by null ptr 72
and this by keith
I have no idea what your point 2 means. Is "&& ||" short for the
English "and/or"? If so, please spell it out. (Even given that
assumption, I don't know what you mean.)

but both are incorrectly quoted and attributed.

Can you PLEASE fix your quotation system? I'm guessing that you're
using MIME encodiing or xml or stripped html or something horrible for
posting, since your posts contain everything twice. STOP THAT.
Use plaintext only.
 
K

Keith Thompson

Null_Ptr_72 said:
Dear Keith,

It's just a pointer, Logical one"so-to-speak". The best way "for
me" to understand any program is to evaluate everyline as to whether or
not it will execute (&& || and/or) under what conditions (True && ||
False) will it execute. if you still want me to draw you a picture, i
will. And besides (Are you here to help and be helped? or scare away a
week old forum members?) Common man!

I'm not trying to scare anyone away. I just found your statement
difficult to understand. It wasn't at all clear that "true and/or
false" refers to whether a statement will be executed. (And your
"&& ||" notation for "and/or" really is hard to read; please consider
dropping it.)

This is intended purely as constructive criticism.
 
A

Arthur J. O'Dwyer

On 19 Apr 2005 08:44:11 -0700, in comp.lang.c , "Null_Ptr_72"

this was written by null ptr 72 [...]
and this by keith [...]
but both are incorrectly quoted and attributed.

Can you PLEASE fix your quotation system? I'm guessing that you're
using MIME encodiing or xml or stripped html or something horrible for
posting, since your posts contain everything twice. STOP THAT.
Use plaintext only.

The OP appears to be posting through Google Groups Beta, which is a
known troublemaker. (The Gmail address tends to be a giveaway, though
I've seen a few people who use throwaway Gmail accounts and not Beta.)
So your advice, while good, is likely to go unheeded.
To the OP: Stop using Google Groups Beta to post to Usenet. It makes
you look like an idiot.

-Arthur
 
K

Keith Thompson

Arthur J. O'Dwyer said:
On 19 Apr 2005 08:44:11 -0700, in comp.lang.c , "Null_Ptr_72"

this was written by null ptr 72 [...]
and this by keith [...]
but both are incorrectly quoted and attributed.

Can you PLEASE fix your quotation system? I'm guessing that you're
using MIME encodiing or xml or stripped html or something horrible for
posting, since your posts contain everything twice. STOP THAT.
Use plaintext only.

The OP appears to be posting through Google Groups Beta, which is a
known troublemaker. (The Gmail address tends to be a giveaway, though
I've seen a few people who use throwaway Gmail accounts and not Beta.)
So your advice, while good, is likely to go unheeded.
To the OP: Stop using Google Groups Beta to post to Usenet. It makes
you look like an idiot.

Google Groups Beta is badly broken, but there are workarounds.
For example, see CBFalcone'rs sig quote:

"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

As far as I can tell, Google Groups Beta does quoting properly if it
does it at all. If the OP quoted a previous article without the "> "
prefix, he probably did his own cut-and-paste, which would be a
problem under any Usenet interface.
 

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
474,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top