Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #9533 > unrolled thread
| Started by | dmjcunha <dmjcunha@gmail.com> |
|---|---|
| First post | 2011-08-18 10:01 -0700 |
| Last post | 2011-08-18 17:44 +0000 |
| Articles | 6 — 6 participants |
Back to article view | Back to comp.lang.c
identation dmjcunha <dmjcunha@gmail.com> - 2011-08-18 10:01 -0700
Re: identation John Gordon <gordon@panix.com> - 2011-08-18 17:09 +0000
Re: identation blp@cs.stanford.edu (Ben Pfaff) - 2011-08-18 10:23 -0700
Re: identation gwowen <gwowen@gmail.com> - 2011-08-19 00:07 -0700
Re: identation James Kuyper <jameskuyper@verizon.net> - 2011-08-19 08:26 -0400
Re: identation Ike Naar <ike@sverige.freeshell.org> - 2011-08-18 17:44 +0000
| From | dmjcunha <dmjcunha@gmail.com> |
|---|---|
| Date | 2011-08-18 10:01 -0700 |
| Subject | identation |
| Message-ID | <23c66ecd-e562-4e3d-afb8-4b428995b493@h9g2000vbr.googlegroups.com> |
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.
[toc] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2011-08-18 17:09 +0000 |
| Message-ID | <j2jh13$31r$1@reader1.panix.com> |
| In reply to | #9533 |
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"
[toc] | [prev] | [next] | [standalone]
| From | blp@cs.stanford.edu (Ben Pfaff) |
|---|---|
| Date | 2011-08-18 10:23 -0700 |
| Message-ID | <878vqqocql.fsf@blp.benpfaff.org> |
| In reply to | #9533 |
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;}
[toc] | [prev] | [next] | [standalone]
| From | gwowen <gwowen@gmail.com> |
|---|---|
| Date | 2011-08-19 00:07 -0700 |
| Message-ID | <24ecf7fd-7c18-4d92-aeaa-d329dbe0f871@b20g2000vbz.googlegroups.com> |
| In reply to | #9535 |
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.
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2011-08-19 08:26 -0400 |
| Message-ID | <j2lkpn$rbg$1@dont-email.me> |
| In reply to | #9544 |
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
[toc] | [prev] | [next] | [standalone]
| From | Ike Naar <ike@sverige.freeshell.org> |
|---|---|
| Date | 2011-08-18 17:44 +0000 |
| Message-ID | <slrn3vfsj4qjrr.st6.ike@sverige.freeshell.org> |
| In reply to | #9533 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web