Suppose we want to arrange n numbers stored in an array such that all negative value occur before the positive ones. What will be the minimum number of exchanges in the worst case ?
I tried it in the following way :
the worst case would be when we have the positives and negatives arranged in an alternate fashion. So, minimum number of exchanges could be (n/2) + (n-2)/2 + (n-4)/2 + ....
here, I am getting confused regarding calculating this sum. after first series of exchanges we could eliminate the last two and then left with n-2 numbers.
Can someone help me out ?
Here's my vision.
Suppose you have $n_1$ of negative numbers. Then, after the procedure you must have all of the $[1..n_1]$ numbers negative. So, the number of operations is $n_1 - h_1$, where $h_1$ is the number of negative elements already in $[1..n_1]$. The same goes about positive numbers - we can place them at the end of the array with $n_2 - h_2$ operation (I believe you can see what the notation mean).
The overall number of operations is $min(n_1 - h_1, n_2 - h_2)$
It's not difficult to see, that the value of the minimum is $n/2$.