3. Example: Ordered Insert

3.1. Input in Sorted Order
3.2. Input in Arbitrary Order:
3.3. General Case
3.4. Inputs in Almost-Sorted Order
3.5. Almost Sorted - version 2

We'll illustrate the process of doing average-case analysis by looking at the ordered insertion algorithm.

We start, as usual, by marking the simple bits O(1).

Next we note that the loop body can be reduced to O(1).

The complexity of the loop condition and body do not vary from one iteration to another.

It can execute

What we don't know are the probabilities to associate with these different numbers of iterations.

When I used this algorithm as part of a spell checking programs, I saw two different examples of possible input patterns:

In the Forum:

(no threads at this time)