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;}