Mentioned on
http://en.wikipedia.org/wiki/Quicksort
Note the left + (right-left)/2 expression. (left + right)/2 would
seem to be adequate, but in the presence of overflow, can give the
wrong answer; for example, in signed 16-bit arithmetic, 32000 +
32000 is not 64000 but -1536, and dividing that number by two will
give you a new pivotIndex of -768 — obviously wrong.
int choose_pivot(int i, int j)
{
- return ((i + j) / 2);
+ return i + (j - i) / 2;
}
/* sort the list from list[m] to list[n] */