I have just worked on implementation of quicksort in c# but then I have faced a such a problem. When I am using my function
static void QS(int[] arr, int left, int right){
int pivot = left;
int temp;
int i = left + 1;
int j = left + 1;
do {
if (arr [i] < arr [pivot]) {
temp = arr [j];
arr [j] = arr [i];
arr [i] = temp;
i++;
}
else{}
j++;
}while(j<=right);
temp = arr [pivot];
arr [pivot] = arr [i - 1];
arr [i - 1] = temp;
}
For an array
int[] arr = { 12, 9, 19, 8, 7, 13, 10, 71, 18, 34, 90, 15, 3 };
I get the results like this:
9, 12, 19, 8, 7, 13, 10, 71, 18, 34, 90, 15, 3.
Hours been spent on this I still can't quite understand why index i does not proceed. Maybe there are more problems than I think.
I omitted recursive calls to concentrate on fuction itself. I am using this pseudo-code:
Partiton(A,l,r)
//[input corresponds to A[l…r]]
p:=A[l]
i:=l+1
for
j=l+1 to r
if A[j] < p
swap A[j] and A[i]
i:=i+1
swap A[l] and A[i‐1]