Earlier quoted context omitted.
Indeed. It's amazing just how difficult it is to write a correct in-place quicksort that handles all edge cases. It won't be short and elegant either. Just go ahead and try it.
http://pastie.org/1367726 -- it's not that hard, really :) * especially with naive pivot from the middle of array
program qsortTest;
const N = 1000;
var
a: array [0..N] of integer;
i: integer;
procedure quicksort(var a: array of integer; l, r: integer);
var
i, j, temp: integer;
pivot: integer;
begin
pivot := a[(l+r) div 2]; i := l; j := r;
while i pivot do dec(j);
if i l then quicksort(a, l, j);
if i
Do for loops in delphi/pascal iterate 'up to' or 'up to and including' ?If they iterate 'up to' then:
It would seem to me that on the initial call to 'quicksort' the '1000' gets passed in to 'r', the value of 'r' then gets passed in to 'j'.
Now if "'a[j]' This should cause the uninitialized value (0?) to end up as the first element in the sorted output array.
I hope I analyzed that correct, I don't have access to a pascal/delphi compiler here.
When you run your code do you see the output starting with a '0' element ?
Is there another element from the input array missing in the output ?
If pascal/delphi loops iterate up-to-and-including did you actually intend to sort an array of 1001 elements ?