As a general rule, generating random input is not a good way to figure out worst-case running time. For example, quicksort runs in O(n log n) on average, but in the worst case it runs in O(n^2). However, even if you generated a huge number of random samples, for moderately large n you would never come anywhere close to the worst case. Instead, try and construct a worst-case input manually.
In this case, it seems that the worst case, assuming that each array has length N, occurs if
L1 = (N,2N,2N+1,...,3N-3,3N)
L2 = (N+1,N+2,...,2N-1,3N-1)
L3 = (1,2,...,N-1,3N-2)
To see why, trace the execution of the algorithm. The first thing that happens is that the first N-1 elements of L3
will get added to L
. Each of these iterations of the loop will have 3 comparisons: two in the first if
statement and one in the second. Note that we need L1[1]<L2[1]
otherwise it will skip the second comparison in the first if
Next will be the element L[1]=N
, which takes one comparison only.
After this come the first N-1 elements of L[2]
, each of which will require two comparisons, one to L1
and one to L3
.
Next come the next N-2 elements from L1
, with one comparison each.
At this point there is only one element left in each list. L3
gets picked first, with 3 comparisons, and then one comparison for L2
, and that's it.
The total is
(N-1)*(3+2+1)+3+1 = 6N - 2
I think this is the worst case, but you might be able to squeeze one more out of it somewhere. Also, I may have made a mistake, in which case somebody here will probably catch it. The next thing you should do is try to actually prove that this is the worst-case running time.
PS This algorithm is not optimal for merging three lists. Picking the smallest element from the front of the three lists should only require 2 comparisons at most, not 3. If you find that L2<L1
and L1<L3
then it's not necessary to compare L2
and L3
since you already know that L2
is smaller.
On edit: it shouldn't be too hard to prove that this is actually the worst case. Assuming none of the lists are empty, the number of comparisons per iteration is:
- 3 if L3 is smallest and L1 < L2
- 2 if L2 is smallest
- 1 if L1 is smallest
That right there gives you an upper bound of N*6, since each list can only be the smallest N times. So completing a proof just requires examining what happens at the end where the lists become empty.