This was a homework problem but I missed the lecture where the lecturer explained the solution and I still can’t figure it out…
If you are given n real numbers in the interval [0,1] (ie x1, x2, x3, …, xn) show that there is an algorithm that runs in worst case linear time that gives a permutation of these n numbers such that the sum of the differences of adjacent numbers is less than 2. The hint given was “buckets”.
Well, you can go this way. Split
[0, 1]intoksegments:[0, 1/k),[1/k, 2/k), …,[(k-1)/k, 1].You now go through your list and put into a new list first all the numbers that fit into 1st segment, than those that fit into the second etc. This can be done in one pass, so
O(n).Consider what is the needed sum now. The diff of the numbers within the same segment is at most
1/k, the number of such differencesn-(k-1). The rest(n-1)diffs are between the numbers from adjacent buckets, altogether (is that clear, why?) not more than1. The sum is bound by(n-k+1)/k + (k-1)/k. You can make if sufficiently small forklarge enough.More details:
Let’s paint our differences into 2 colours: differences between the numbers from the same segment are painted blue, and differences between the numbers from different segments are painted red. Thanks to the ordering, we have first only the numbers in the 1st segment (possibly 0), than the numbers in the 2nd segment, and so on. So, we have first some blue differences, than a red difference, than again several blue differences, than again a red difference etc.
Let’s see what is the number of red differences. There is obviously not more than
k-1red differences, right? Because each red difference jumps from one segment to a higher segment. The rest of the differences is blue.Now, each blue difference is not more than
1/k, because the minuend and subtrahend are from the same segment. And all the red differences altogether (that is, their sum!) do not exceed 1. (Skipping the rest for now, as this is a homework question.)Correction:
The sum of all red differences doesn’t exceed 2-2/k. Why is that? Well, let’s see. The first red difference may be in the worst case from the beginning of some segment
Ato the end of some other segmentB. The second is in the worst case from the beginning ofBto the end of some other segmentC. So, in the sum of the differences every segment except the first and the last is counted at most twice.