Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > identation

Reply
Thread Tools

identation

 
 
dmjcunha
Guest
Posts: n/a
 
      08-18-2011
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.
 
Reply With Quote
 
 
 
 
John Gordon
Guest
Posts: n/a
 
      08-18-2011
In <(E-Mail Removed)> dmjcunha <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      08-18-2011
dmjcunha <(E-Mail Removed)> 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[]="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;}
 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      08-18-2011
On 2011-08-18, dmjcunha <(E-Mail Removed)> wrote:
> #typedef int Item
> #define key(A) (A)
> #define less(A, B) (key(A) < key(B)


The parentheses don't match.
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      08-18-2011
(E-Mail Removed) (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.

 
Reply With Quote
 
gwowen
Guest
Posts: n/a
 
      08-19-2011
On Aug 18, 6:45*pm, (E-Mail Removed)-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.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      08-19-2011
On 08/19/2011 03:07 AM, gwowen wrote:
> On Aug 18, 6:45�pm, (E-Mail Removed)-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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ElementTree and proper identation? John Salerno Python 8 09-27-2006 06:46 PM
Correct Identation/Contex can solve the too many compiler error messages problem when a closing bracket is missing. Skybuck Flying C Programming 106 09-26-2005 03:56 PM
Correct Identation/Contex can solve the too many compiler error messages problem when a closing bracket is missing. Skybuck Flying Java 107 09-26-2005 03:56 PM



Advertisments