When an array of elements is given, how can I count the number of element comparisons performed by the algorithm?
It is not as simple as adding a counter to the splitting method.
private void partition(int low, int high) {
int i = low, j = high;
int pivot = numbers[low + (high-low)/2];
while (i <= j) {
if (array[i] < pivot) {
i++;
compareCount++;
}
else if (numbers[j] > pivot) {
j--;
compareCount++;
}
else (i <= j) {
exchange(i, j);
i++;
j--;
}
}
You cannot do this because it counts the comparison just made, not the comparison made when evaluating false.
I tried changing if (array[i] < pivot)
to while (array[i] < pivot)
(for j, too), but I feel like I'm still missing something if I do this.
source
share