So I have an insertion sort function implemented that sorts through an array, but I'm having a problem showing the correct number of comparisons to work.
Each time I'm checking a value with another, the counter should update.
For instance, having an array of 10 elements going from 10-1 should give 45 comparisons, but I'm getting 54 comparisons.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void insertionSort(int a[], int& comparisons, constint& numOfElements)
{
int j, value;
for (int i = 1; i < numOfElements; i++)
{
value = a[i];
for (j = i - 1; j >= 0 && a[j] > value; j--)
{
a[j + 1] = a[j];
comparisons++;
}
comparisons++;
a[j + 1] = value;
}
}