hi, i have a functionn sort_and_count where i will sort a vector and count the number of inversions. i will first divide the list into two n call the function recurrsively. problem is its going on,doesnt stop. please can you tell me why is this problem here? this implements merge sort also.
here's the sample code:
 int sort_and_count(int Left,int Right)

{

int Mid;

int count=0,countL=0,countR=0;

if(Left==Right)

{

return count=0;

}

else

{

Mid=(Left+Right+1)/2;

countL = sort_and_count(Left,Mid);

countR = sort_and_count(Mid+1,Right);

count = merge_count(Left,Mid,Right);

return count=count+countL+countR;

}

}
thank you in advance..
There is something wrong with your equation
1 = (0 + 1 + 1)/2 // Integral math
In the next line you state sort_and_count(Left,Mid); since Mid == Right, you’ll get into an infinite loop here.
Any time the difference between left and right is 1, then Mid == Right
therefore you’ll never get to the base case of left == right