[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

identation

dmjcunha

8/18/2011 5:02:00 PM

Hi. The code below is exactly as it is in the book Algorithmsin c
third edition. I just would like to know if there was an error of
identation in the compexch.

#typedef int Item
#define key(A) (A)
#define less(A, B) (key(A) < key(B)
#define exch(A, B) {Item t = A; A = B; B = t;}
#define compexch(A, B) if (less(B, A)) exch(A, B)
#define M 10

void quicksort(Item a[], int l, int r)
{int i;
if(r - l <= M) return;
exch(a[(l + r) / 2], a[r - 1]);
compexch(a[l], a[r - 1];
compexch(a[l], a[r]);
compexch(a[r - 1], a[r]);
i = partition(a, l + 1, r - 1);
quicksort(a, l, i - 1)
quicksort(a, i + 1, r);
}

Also if someone could give me a clue of how to make it based on
partitioning on the median of a random sample of five elements and the
elements of the sample do not participate in partitioning instead of a
median of three algorithm I would enjoy because I don't have the
minimum idea of how to make it. I was thinking if the identation was
wrong I should put statements like
ran = rand() % (r - l);
compexch(a[l], a[ran]);

ran = rand() % (r - l);
compexch(a[l + 1], a[ran])

and so on and call
i = partition(a, l + 5, r)
but I really don't know.
6 Answers

John Gordon

8/18/2011 5:10:00 PM

0

In <23c66ecd-e562-4e3d-afb8-4b428995b493@h9g2000vbr.googlegroups.com> dmjcunha <dmjcunha@gmail.com> writes:

> Hi. The code below is exactly as it is in the book Algorithmsin c
> third edition. I just would like to know if there was an error of
> identation in the compexch.

Indentation makes no difference to the code; it is only for readability.

--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

blp

8/18/2011 5:23:00 PM

0

dmjcunha <dmjcunha@gmail.com> writes:

> Hi. The code below is exactly as it is in the book Algorithmsin c
> third edition. I just would like to know if there was an error of
> identation in the compexch.

There is silly indentation, but there are plenty of other
problems and questionable practices also. I would not use this
code for anything.

Here are a few problems I noticed quickly. First, there's no
"#typedef" preprocessor directive. Evidently "typedef int Item;"
is what was meant:
> #typedef int Item
> #define key(A) (A)
> #define less(A, B) (key(A) < key(B)

The following macro expands its arguments multiple times and
fails to protect itself with do...while(0):
> #define exch(A, B) {Item t = A; A = B; B = t;}

Ditto:
> #define compexch(A, B) if (less(B, A)) exch(A, B)

"M" is a pretty lousy name for a macro:
> #define M 10

'l' is a lousy name for a variable, especially in a function that
also uses the integer '1':
> void quicksort(Item a[], int l, int r)

This indentation style is weird:

> {int i;
> if(r - l <= M) return;
> exch(a[(l + r) / 2], a[r - 1]);

It is very unconventional to indent in the following "stair-step"
way:
> compexch(a[l], a[r - 1];
> compexch(a[l], a[r]);
> compexch(a[r - 1], a[r]);
> i = partition(a, l + 1, r - 1);
> quicksort(a, l, i - 1)
> quicksort(a, i + 1, r);
> }

--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
\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;}

Ike Naar

8/18/2011 5:44:00 PM

0

On 2011-08-18, dmjcunha <dmjcunha@gmail.com> wrote:
> #typedef int Item
> #define key(A) (A)
> #define less(A, B) (key(A) < key(B)

The parentheses don't match.

ram

8/18/2011 5:45:00 PM

0

blp@cs.stanford.edu (Ben Pfaff) writes:
>It is very unconventional to indent in the following "stair-step"
>way:
>> compexch(a[l], a[r - 1];
>> compexch(a[l], a[r]);
>> compexch(a[r - 1], a[r]);

It could become more conventional by using blocks:

compexch(a[l], a[r - 1];
{ compexch(a[l], a[r]);
{ compexch(a[r - 1], a[r]); }}

But now, one can comment that it is »very unconventional« to
use blocks as above, since they are redundant.

However, redundant blocks might sometimes be used like
comments, to group or subordinate parts of the source code
for a human reader. But this might not apply, here.

gwowen

8/19/2011 7:07:00 AM

0

On Aug 18, 6:45 pm, r...@zedat.fu-berlin.de (Stefan Ram) wrote:

>   It could become more conventional by using blocks:
>
> compexch(a[l], a[r - 1];

Are you sure about this?

I've looked in my 2nd (1990) edition and all the sorting algorithms I
see there are just perfomed on int[], rather than some abstract keyed
datatype (which are mentioned only briefly as motivation for the
"Radix Sort" chapter). My guess would be that the macro kludge given
above were a late addition to the 3rd edition, and not tested beyond
"that looks right". (Score +1 for overloading operator< on this one).

The indentation is lot better in the earlier edition, too.

James Kuyper

8/19/2011 12:27:00 PM

0

On 08/19/2011 03:07 AM, gwowen wrote:
> On Aug 18, 6:45�pm, r...@zedat.fu-berlin.de (Stefan Ram) wrote:
>
>> � It could become more conventional by using blocks:
>>
>> compexch(a[l], a[r - 1];

It seems a little peculiar to concentrate on his statement about blocks,
without also retaining the blocks that he was talking about.

> Are you sure about this?

Well, the unmatched parenthesis is obviously wrong, but his comment
about using blocks was correct. However, keep in mind that "more
conventional" is also, in this particular case, still "highly
unconventional", and also "not recommended".
--
James Kuyper