D
dmjcunha
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.
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.