重要な点は、ピボットをスワッピングから外しておくことです。ただし、境界線の1つに格納したときと最後に、正しい場所にスワップするときは、最初の点を除きます。
int partition(int *A, int low, int high) {
if (high <= low) return low;//in that case, partition shouldn't be called, but cater for it
int pivot_position = low; //or high, or low + (high-low)/2, or whatever
int pivot = A[pivot_position];
A[pivot_position] = A[high];
A[high] = pivot; //these two lines are unnecessary if pivot_position == high
int i = low, j = high-1;
while(i < j) {
while(i < j && A[i] <= pivot)
++i; //i == j or A[i] > pivot, and A[k] <=pivot for low <= k < i
while(i < j && A[j] > pivot)
--j; //i == j or A[j] <= pivot, and A[m] > pivot for j < m < high
if (i < j) {
int temp = A[j];
A[j] = A[i];
A[i] = temp;
}
}
if (A[i] <= pivot)
++i;
//now A[k] <= pivot for low <= k < i and A[m] > pivot for i <= m < high
A[high] = A[i];
A[i] = pivot;
return i;
}