# How to count sort comparison

When it is said that this sort has M number of comparisons what does it mean?

As an example:

``````procedure bubbleSort( A : list of sortable items )
n = length(A)
repeat
swapped = false
for i = 1 to n-1 inclusive do
if A[i-1] > A[i] then
swap( A[i-1], A[i] )
swapped = true
end if
end for
until not swapped
end procedure
``````

Has

``````for i = 1 to n-1
``````

or

``````for(int i = 0 ; i < n-1; i++)
``````

is this comparison (`i < n-1`) taken into account?

Or in merge sort, beside the main comparison:

``````        if (v[first1]<v[first2])
``````

at the end of a used function it is written:

``````    while (first1 <= last1)
temp[index++] = v[first1++];
while (first2 <= last2)
temp[index++] = v[first2++];
for (index = first; index <= last; index++)
v[index] = temp[index];
``````

Are these comparisons are taken into account?

• Possible duplicate of programmers.stackexchange.com/questions/117304/… Apr 24, 2015 at 0:37
• @JayElston, Could you explain how that question answers about what commands to be taken into account? btw, I do not care about big o. I need exact number of comparison. Apr 24, 2015 at 4:01