3.5. Almost Sorted - version 2

Now, that's only one possible scenario in which the inputs are almost sorted. Let's look at another. Suppose that we knew that, for each successive input, the probability of it appearing in the input `m` steps out of its correct position is proportional to `1/(m+1)` (i.e., each additional step out of its correct position is progressively more unlikely). Then we have

`p_{0}=c, p_{1}=c/2, p_{2}=c/3, \ldots p_{n-1}=c/n, p_{n}=c/(n+1)`

The constant `c` is necessary because the sum of all the probabilities must be exactly 1. We can compute the value of `c` by using that fact:

`\sum_{i=0}^{n} p_i = 1`

`\sum_{i=0}^{n} \frac{c}{i+1} = 1`

`c \sum_{i=0}^{n} \frac{1}{i+1} = 1`

This sum, for reasonably large n, is approximately `log n`.

So we conclude that `c` is approximately `= 1/log(n)`.

So the function, for this input distribution, is

`t_{text{seqIns}} = O(k_{text{avg}})

`\ \ \ = O(\sum_{i=0}^n (i + 1)p_i)`

`\ \ \ = O(\sum_{i=0}^n (i + 1) c/(i+1})`

`\ \ \ = O(\sum_{i=0}^n c)`

`\ \ \ = O((n+1)c)`

`\ \ \ = O(n/(log n))`

S o the average case is slightly smaller than the worst case, though not by much (remember that `log n` is nearly constant over large ranges of `n`, so `n/(log(n))` grows only slightly slower than `n`.


In the Forum:

(no threads at this time)