I have a hard time translating QuickSort with Hoare partitioning into C code, and can\'t find out why. The code I\'m using is shown below:
void QuickSort(in
I believe that there are two problems with this code. For starters, in your Quicksort function, I think you want to reorder the lines
int q=HoarePartition(a,start,end);
if (end<=start) return;
so that you have them like this:
if (end<=start) return;
int q=HoarePartition(a,start,end);
However, you should do even more than this; in particular this should read
if (end - start < 2) return;
int q=HoarePartition(a,start,end);
The reason for this is that the Hoare partition fails to work correctly if the range you're trying to partition has size zero or one. In my edition of CLRS this isn't mentioned anywhere; I had to go to the book's errata page to find this. This is almost certainly the cause of the problem you encountered with the "access out of range" error, since with that invariant broken you might run right off the array!
As for an analysis of Hoare partitioning, I would suggest starting off by just tracing through it by hand. There's also a more detailed analysis here. Intuitively, it works by growing two ranges from the ends of the range toward one another - one on the left-hand side containing elements smaller than the pivot and one on the right-hand side containing elements larger than the pivot. This can be slightly modified to produce the Bentley-McIlroy partitioning algorithm (referenced in the link) that scales nicely to handle equal keys.
Hope this helps!