I've learned Haskell, and now I'm beginning to learn C. I've been trying to get my quicksort code in Haskell to C but I did not succeed. So I decided to look at some books and found the following code:
/* Função de inicialização da Quicksort. */
void quicksort(char *item, int count)
{
qs(item, 0, count-1);
}
/* A Quicksort. */
void qs(char *item, int left, int right)
{
register int i, j;
char x, y;
i = left; j = right;
x = item[(left + right)/2];
do
{
while(item[i]<x && i<right) i++; // DUVIDA 1
while(x<item[j] && j>left) j--; // DUVIDA 1
if(i<=j)
{
y = item[i];
item[i] = item[j];
item[j] = y;
i++; j--;
}
}
while(i<=j); // DUVIDA 2
if(left<j) qs(item, left, j);
if(i<right) qs(item, i, right);
}
Doubt 1) I did some table tests, I compiled and executed with some entries and I did not find out why to put && i<right
and && j>left
within the while whereas item[i]<x
and x<item[j]
already do the due checks.
Doubt 2) For this code it would not be more correct to only put while(i<=j)
instead of do while(i<=j)
.