1. Introduction
“One Picture is Worth a Thousand Words”
[An advertisement for the San Antonio Light (1918)]
Teaching undergraduate Analysis of Algorithms has been a rewarding, although a bit taxing, experience. I was often surprised to learn that many basic problems that clearly belong to its core syllabus had been left unanswered or partially answered. Additionally, it seemed a bit odd to me that many otherwise decent texts offered unnecessarily imprecise computations (text [
1] being a notable exception in this category) of several rather fundamental results.
In this article, I pursue a seemingly marginal—albeit fundamental—topic, a precise characterization of the best-case behavior of a well-known sorting algorithm, , whose pursuit, however, yields some interesting findings that could hardly be characterized as “marginal.” It turns out that—contrary to what a casual student of this subject might believe—computing the exact formula for the number of comparisons of keys that performs on any n-element array in the best case is not a routine exercise (in fact, it is significantly more complicated than precise derivation of the well-known exact formula for the number of comparisons of keys performed by it in the worst case) and leads to an instance of a problem that gained some notoriety for being a hard nut to crack analytically: the sum of digits problem. Even more unexpectedly, the relatively straightforward (although not quite closed-form) formula for the said number of comparisons yields an improvement of a well-known answer to this instance of the sum of digits problem:
How many 1s appear in binary representations of all integers between (but not including) 0 and n?
2. Materials and Methods
Several derivations presented in this article may be characterized as experimental mathematics. Once they produced experimentally (using Wolfram Mathematica software) the desired (sought-after) formulas, finding analytic proofs of the said formulas became a relatively easy and straightforward exercise, and has been done in
Section 9,
Section 10 and
Section 11. Those derivations provide an insightful exercise that demonstrates how certain, apparently, hard-to-solve recurrence relations pertaining to the complexity of divide-and-conquer algorithms can be practically and precisely solved by means of experiments and computations augmented with formal analytic proofs of the derived solutions.
No materials were used in the research described in this article.
3. Results
The main result of this article is the following formula of the Main Theorem that gives the exact number
of comparisons of keys performed by the classic
algorithm in the best case while sorting an
n-element array.
where
The above formula may be evaluated in sublinear
time, where
c is a constant, that is considerably shorter than the time needed for running
on its best-case input array (for instance, an array of consecutive integers) of
n elements or for counting directly how many times digit 1 occurs in binary representations of all integers between 1 and
n. This constitutes a significant improvement over other exact formula, attributed to J. R. Trollope and discussed in
Section 8, for the said number of 1s that involves infinite summation. Even if only finitely many terms in that infinite summation are non-zero, the Trollope formula does not indicate how long the process of its computation will take for any value of
n. For instance, the Trollope formula may possibly require more time to evaluate than counting directly how many times digit 1 occurs in binary representations of all integers between 1 and
n. (See Note 1 at the end of this Section.)
The exactness of the formula derived and proved in this article allows for its definite experimental verification, which in general cannot be done for characterizations given by Big-O/Big-
/Big-
notation. Such an experimental verification has been performed by the uthor using a Java program that incorporates the code for
visualized in
Figure 1.
Moreover, the exact and practical way to compute the answer to the question of how many comparisons of key does perform in the best case while sorting an n-element array fills a significant loophole in many computer science faculty and students’ knowledge of the time complexity of and satisfies the cognitive curiosity of those who realize the advantages of knowing things exactly in exact sciences and mathematics. (See Note 2 at the end of this Section.)
It is also demonstrated (see Corollary 3) that there is no exact closed-form formula that gives the said number of comps and (in
Section 7) that the said number is significantly larger (by a term of size
) than half of the number of comps performed by
in the worst case.
Note 1. Since the total number of significant bits in binary representations of all numbers between 1 and n is equal to + 1), the direct counting of 1s in all those numbers requires at least or, by virtue of Stirling’s formula, steps that take time to carry them on, which is significantly more than the time that is sufficient for the evaluation of Formula (15) derived and proved in this article. Note 2. A student of mine was once trying to convince me that the number of comparisons of keys that performs in the worst case was exactly equal to twice the number it performed in the best case. In order to show her why it was not exactly the case, I experimentally derived an exact formula for it. This is how I got into writing this article.
4. and Its Best-Case Behavior
A call to inherits an n-element array of integers and sorts it non-decreasingly, following the steps described below.
A Java code of
is shown in
Figure 1.
A typical measure of the running time of is the number of comparisons of keys, which for brevity I call comps, that it performs while sorting array . Since no comps are performed outside , the running time of can be computed as the sum of numbers of comps performed by all calls to during the execution of .
Because all comps during the execution of
are performed within the
-loop that begins in line 76 of the code shown in
Figure 1, and the said loop does not terminate before the last element of one of the two arrays has been compared with an element of the other array, the number of comps performed by
on two arrays is never less than the size of any of the two arrays passed to it, which in the case of step 2c of Algorithm 1 is not less than
, thus making
a lower bound for the said number of comps. Since the number of comps performed by
in the case where all the elements of the first array of size
are less than all the elements of the second array of size
is actually equal to the size of the first array, it follows that
is the minimum number of comps performed during the execution of
(and not just a lower bound of that number).
| Algorithm 1 |
| To sort an n-element array , do the following: |
Obviously, increasingly sorted array
of any size
produces only best-case scenarios for all subsequent calls to
. Therefore,
—where
is the size of any sub-array
of size greater than or equal to 2 passed to any (recursive) call to
—is the actual minimum of and not just a lower bound on the said number of comps performed by
invoked by the said call to
. This fact allows for a rudimentary analysis of the recursion tree for
that easily yields—as we will see in the next two subsections—the exact Formula (
7) for the minimum number of comps for the entire
. Without said fact, one could only derive by means of analysis of the recursion tree a lower bound on the number of comps performed by
, even though—as I have pointed out, above—the size
of the first sub-array passed to
is the actual minimum of comps performed at this level and not just a lower bound.
The problem arises when one tries to reduce the said formula, which naturally involves long summations, to one that can be evaluated in a logarithmic time.
4.1. Recursion Tree
The obvious recursion tree for
and sufficiently large
n is shown in
Figure 2 and explained in some detail in the caption below it.
A recursive application of the equality
(which can be easily verified separately for odd and even values of
n) allows for rewriting of that tree onto one whose first four levels are shown in
Figure 3.
4.2. Recurrence Relation for the Minimum Numbers of Comps
As I have noticed, before, the number of comps performed by a single call to in the best case on an n-element array for is equal to . (Recall that all comps performed during a single call to are done within the call to that invokes after both recursive calls to itself have been completed).
Therefore, the following recurrence relation for the minimum number
of comparisons of keys that
performs on any
n-element array is straightforward to derive from its description given by Algorithm 1, as follows:
and for
,
Using the equality (
1), the recurrence relation (
3) is equivalent to
A graph of
is shown in
Figure 4.
Note 3. If n is a power of 2, that is, if , thenIndeed, in such a case, recurrences (3) and (4) simplify to , so that [
since and, by the equality (2), ]
. 4.3. A Solution of Recurrence Relation
Unfolding the recurrence (
4) allows for noticing that the minimum number
of comps performed by all calls to
is equal to the sum of all values shown at nodes highlighted yellow in the recursion tree
T of
Figure 3. They may be summed up level by level. One can notice from
Figure 3 that the number of comps performed at any level
k with the maximal number
of nodes is given by this formula:
What is not clear is whether all levels of the recursion tree
T are maximal. Fortunately, the answer to this question does not depend on whether a given instance of
is running on a best-case array or on any other case of array. It has been known from a classic analysis of the worst-case running time of
that every level of its recursion tree
T that contains at least one non-leaf, or—in other words—a node that shows the value
, is maximal.
Appendix A, contains a detailed derivation of that fact. Thus all levels 0 through
of
T are maximal. Therefore, Formula (
5) gives the number of comps for every level
.
The last level
h of
T may be not maximal because the level
may contain leaves, or —in other words—nodes that show the value
, where
for some
, and as such do not have any children in the level
h. However, for each such node, the value of
is 0, so it can be included in summation (
5) without affecting its value even though the said value does not correspond to any node in the level
h. Therefore, Formula (
5) gives the number of comps for the level
.
Additionally, the depth
h of
T is known to be equal to
, as Theorem A2 in
Appendix A states. Thus the minimum number of comps performed by
is given by this summation
Since
so that
and for every
,
, thus making
it follows that the value of the inner summation (
5) in summation (
6) for
is 0. Therefore, Formula (
6) is equal to this slightly shorter formula:
Unfortunately, summation (
7) contains
non-zero terms (each corresponding to one of
internal nodes of 2-tree
T), so it cannot be evaluated quickly in its present form. Fortunately, as I am going to demonstrate, its inner summation (
5) can be reduced to a closed-form formula (
13).
4.4. Zigzag Function
In order to reduce (
5) to a closed form, I am going to use the function
Zigzag defined by
The following fact is instrumental for that purpose.
Theorem 1. For every natural number n and every positive natural number m,where Zigzag is a function defined by (8) and visualized in Figure 5. Proof. The equality (
9) can be verified experimentally, for instance, with the help of software for symbolic computation; I used Wolfram Mathematica for that purpose. The analytic proof is deferred to
Section 9. □
Corollary 1. For every natural number n and every positive natural number m,where Zigzag is a function defined by (8) and visualized in Figure 5. Proof. First, let us note (analytic proof of this fact is a straightforward exercise; see
Appendix B) that
From (
11), I conclude
Solving Equations (
9) and (
12) for
yields (
10). □
Here is the closed form of summation (
5).
Corollary 2. For every natural number n and every natural number k,where Zigzag is a function defined by (8) and visualized in Figure 5. Proof. Substitute
in (
10). □
The following theorem yields Formula (
14) for the minimum number
of comps performed by
.
Theorem 2. For every natural number n,where Zigzag is a function defined by (8) and visualized in Figure 5. Formula (
14), although not a quite closed form, comprises summation with only
closed-form terms, so it may be evaluated in
time, where
c is a constant. I will show in
Section 5 that (
14) does not have a closed form. Graphs of both sides of equality (
14) are shown in
Figure 6. One can see that, for natural numbers
n, they coincide with the solution
of recurrences (
2) and (
3) visualized in
Figure 4.
Main Theorem. For every natural number n, the minimum number of comps that performs while sorting an n-element array iswhere Zigzag is a function defined by (8) and visualized in Figure 5. Proof. As it has been shown in
Section 4.3, Formula (
7) gives the minimum number
of comps performed by
on any
n-element array. Thus substituting
for
in equality (
14) of Theorem 2 yields (
15). □
5. A Fractal in
A deceitfully simple expression
half of which occurs in Formula (
15) of the Main Theorem, is a formidable adversary for those who may try to turn it into a closed form, although the time required for its evaluation for any given
n is
(so, for all practical purposes, some may consider (
15) a closed-form formula). That does not come as a surprise, taking into account that its graph, shown in
Figure 7, bears a resemblance of fractal. This can be easily seen as soon as a sawtooth function
is subtracted from it, yielding the function
given by
Since
, equality (
8) implies
or
Equality (
18) simplifies definition (
17) of function
F to
visualized in
Figure 8.
The function
F is a fractal with quasi similarity that repeats at intervals of exponentially growing length. It is a union
of functions
, each having an interval
as its domain. In other words, for every integer
,
which, of course, yields (
20).
Let
be the normalized
on interval
, defined by
and
be the periodized
by composing it with a sawtooth function
(the fractional part of
x) defined by
Contracting definitions (
21), (
22), and (
23) yields
One can compute (an elementary geometric argument based on the graph visualized in
Figure 9 will do) from (
24) the following alternative formula for
:
Figure 9 shows functions
drawn on the same graph.
Since each function
, and—therefore—each function
, and—therefore—each function
, are a result of smaller and smaller triangles piled, originating in the function
Zigzag of definition (
19) of function
F, on one another as shown in
Figure 9, for any integers
,
linearly interpolates
. Because of that, each
linearly interpolates the limit
of all
s defined by
as
Figure 10 illustrates. An application of (
25) to (
26) yields
Since, for every integer
n,
k and
,
is integer,
. Therefore, by virtue of (
25) and (
27), for every non-negative integer
k and
n,
This and (
25) eliminate the need for infinite summation (as it appears in (
27)) while computing
.
It can be shown that, although a continuous function,
—known under the name of the blancmange function or the Takagi fractal curve—is nowhere differentiable. As such, it does not have a closed-form formula as any closed-form formula on a real interval must define a function and have a derivative at every point of that interval, except for a non-dense set of its points. Since
can be expressed in a function, described by a closed-form formula, of the right-hand side of Formula (
14), the latter does not have a closed-form formula either.
Theorem 3. For every closed-form formula there is a positive n such thatwhere Zigzag is a function defined by (8) and visualized in Figure 5. Proof. Follows from the above discussion. A more detailed analytic proof is deferred to
Section 10. □
This way, we arrived at the following conclusion.
Corollary 3. There is no closed-form formula for .
Proof. A closed-form formula for
would, by virtue of (
15) yield a closed-form formula for
, which by Theorem 3 does not exist. □
Note 4. One can apply the reverse transformations to those used in Section 5 on the function and construct a fractal function , shown in Figure 11, given by the equationwhich for every positive integer n satisfieswhere F is given by (19). 6. Computing and from One Another
Computing values of the function
does not have to be as complex as (or more complex than) definition (
27) implies. Of course, for every integer
n,
= 0. One can apply some elementary arguments based on a structure visualized in
Figure 10 to conclude that
(the latter being the maximum of
) or that, for every positive integer
k,
It takes a bit more work to compute
It turns out that computing values of the function
for every
x that has a finite binary representation can be done easily if an oracle for computing the values of the function
defined by (
2) and (
4) is given, which is not that surprising after a glance in
Figure 11. Once that is accomplished, since
is a continuous function and the set of numbers with finite binary representations is dense in the set
of reals, it allows for fast approximations of
for every real
x. (It helps to remember that
is a periodic function with
.)
Theorem 4. For every positive integer n and integer k with , Proof. Equality (
35) can be verified experimentally, for instance, with the help of software for symbolic computation (
Section 4.4). The analytic proof is deferred to
Section 11. □
Theorem 4 allows for easy computing of
if
is given for some
using this form of (
35):
Corollary 4. For every positive integer n and integer k with , Proof. An obvious conclusion from (
35). □
For instance, putting
in (
36) easily yields (
15). For
, I obtain
[by (
27)]
[since for
,
is integer and
]
Substituting
k for
, I conclude
similar to the (
15) characterization of
.
7. Relationship Between the Best Case and the Worst Case
An attentive student of
tends to believe (taking into account that substituting
for
in the recurrence relation (
3) for the best case yields the recurrence relation for the worst case) that its worst-case behavior is about twice as bad as its best-case behavior. This, of course, is only approximately true. In this section, I will derive the exact difference between
and
using the function
F defined by (
17).
An exact formula for the number
of comparisons of keys performed by
in the worst case is known (see Formula (
A7) in
Appendix A) and is given for any positive integer
n by the following equality:
From (
15) and (
17), one can derive
[by
from [
3]
[by (
38)]
The above equality yields the following characterization.
Theorem 5. For every positive integer n, the difference between twice the number of a comparison of keys performed in the best case and the number of a comparison of keys performed in the worst case by while sorting an n-element array iswhere , visualized in Figure 8, is given by (19). Proof. Follows from the above discussion. □
In particular, since, for every positive integer
n,
(see
Figure 8 for an explanation), I conclude with the following tight linear bounds on
.
Corollary 5. For every positive integer n, the difference between twice the minimum number and the maximum number of a comparison of keys performed in the worst case by while sorting an n-element array satisfies this inequality: Proof. Follows from (
39) and (
40). □
Obviously, whenever , that is, whenever . It can be shown that whenever n= for some integer . Therefore, the number of comps performed by in the best case is larger than half the number of comps performed by it in the worst case by at least by and at most by , which makes the former number significantly larger (by a term of size ) than half of the latter number.
A graph of
and its tight bounds are shown in
Figure 12.
8. The Sum of Digits Problem
A known [but not to me during my derivation of Formula (
15)] explicit formula, published in [
4], for the total number
of 1s in binary representations of all integers between 0 and
n (not including 0 and
n) is expressed in terms of the function
Zigzag (referred to as
in [
4]). The following are verbatim quotations and screenshots from [
4].
“If denotes the sum of the digits of when is represented in base r, then”
![Mathematics 14 00733 i001 Mathematics 14 00733 i001]()
“Let be periodic of period 1 and defined on by”
![Mathematics 14 00733 i002 Mathematics 14 00733 i002]()
It has been shown in [
5] that the recurrence relation for
is the same as the recurrence relation for
given by (
2) and (
3). Therefore, Formula (
14) derived in this article is equivalent to
given above by the considerably more complicated definition. Interestingly, the above definition can be simplified to (
14) along the lines of the elementary derivation of the alternative Formula (
37) for
.
Even more interestingly, if someone did succeed in simplifying Trollope’s formula in [
4] then I am not aware of it. In particular, the most recent article [
6] on the subject of sum of digits does not contain any hint that such a simplification has been found and published.
9. Proof of Theorem 1, Section 4.4
In this section, I provide an analytic proof of the experimentally derived Theorem 1,
Section 4.4, which was instrumental for the derivation of a logarithmic-length Formula (
15) for
. The result and its proof have a flavor of Concrete Mathematics. Although they are interesting in their own right, they cannot be found in [
7].
Theorem 6. (Same as Theorem 1.)
For every natural number n and every positive natural number m,where Zigzag is a function defined by (8) and visualized in Figure 5. Proof. First, let us note that
that is,
Let
where
, and let
. We have
and
Thus, by virtue of (
43),
We have
because
so that
, and therefore,
.
Let
I be defined as
By virtue of (
46), we have
where
denotes the cardinality of
I.
If
, then, by (
47),
. If
, then, by (
47),
. In any case,
[since
so that
and
]
[by definition (
8) of the function
Zigzag]
[since
Zigzag is a periodic function with period 1]
[by (
44)]
Thus
From (
43), (
45), (
48), and (
49), I conclude (
42). □
10. Proof of Theorem 3, Section 5
In this section, I provide an analytic proof of Theorem 3.
I begin with a brief discussion/motivation of what can be generally considered a closed-form formula for a function from the set of real numbers, except, perhaps, a finite number of reals, into a set of real numbers.
The general concept of closed-form formula (cff) has not been precisely defined in the literature. In this article, it denotes a formula that is composed of constants, variables, and some “standard” arithmetic and logical operations that has a finite and fixed structure and length that do not depend on the values of any variables occurring in the said formula. More specifically, in this article, it is used in the following sense.
The arithmetic constants are any integers with fixed values. The logical constants are true and false.
The basic arithmetic functions are: binary addition, binary multiplication, binary maximum and minimum, subtraction, division, exponentiation, logarithm functions, and the floor function. The basic arithmetic relations are: the equality relation and the less-than relation. The basic logical operations are: negation and binary conjunction.
An atomic cff is an expression that is a constant or variable, or denotes a reference to a basic function or relation whose arguments are constants or variables. The set of cffs is the smallest set of expressions that contains all atomic formulas and is closed under any finite and syntactically correct applications of basic functions and relations and the conditional arithmetic operation by the statement if c then d else e, where c is a logical cff and d and e are arithmetic cffs.
Note 5. One could use a more inclusive definition of closed-form formula without invalidating the results presented in this article as long as the extra arithmetic expressions allowed by it are differentiable on their domains except, perhaps, on their non-dense subsets.
Since non-rational real numbers are defined as limits of infinite, Cauchy-convergent rationals, limits of certain infinite sequences of reals do implicitly occur in definitions of some functions the references to which are accepted as
cffs. Below is a motivating example of the function
as an insight of what is accepted as a
cff for a
continuous function—like, say,
—on the set
of reals or on an interval thereof. One picks a dense (in the metric topology of
) subset
(in this example, the set of rationals) of
, with a collection of mappings
, where
, given by
so that
. Since, for any
,
has been defined as
is considered a
cff for the function
.
For the reader’s convenience, Theorem 3 is quoted below as Theorem 7.
Theorem 7. (Same as Theorem 3).
There is no cff the domain of which contains all positive integers and the values of which coincide with for all positive integers n; that is, for every cff , there is a positive n such thatwhere Zigzag is a function defined by (8) and visualized in Figure 5. The rest of this section constitutes the proof of Theorem 7.
Lemma 1. For every positive integer n,where the function F has been defined by equality (17); the function , visualized in Figure 13 has been defined by equality (27); and the function has been defined by Equations (2) and (4). Proof. From (
17) I compute
that is,
Applying equality (
53) to equality (
15) I conclude
or
that is,
or
On the other hand, by virtue of (
19),
[putting
]
[since for
,
so that
]
[by (
27)]
Thus
or
Combining equalities (
54) and (
56) yields
or (
52). □
Lemma 2. If the function defined by equations (2) and (4) has a cff , then the function defined by equation (27) has a cff . Proof. Let
be the set of rationals in the interval
with finite binary representations, enumerated by
given by
and visualized in
Figure 14. (It is a trivial exercise to show that every real number with finite binary representation in the interval
is of the form
for some
, and it is obvious that every real number of that form has a finite binary representation and falls into that interval).
D is a dense subset of the interval
of reals. Indeed, if
, then, for every
,
and
Hence, for any
, putting
so that
[the last equality holds because
so that
and
], or
I conclude, by virtue of (
58),
[by the continuity of
]
[by equality (
52) of Lemma 1]
[by equality (
58)]
Thus for any
,
Equality (
61) shows that if there is a
cff for the function
B defined by Equations (
2) and (
4) then there is a
cff given by
for
. This completes the proof of Lemma 2. □
Should the nowhere differentiable function have a cff, it would be differentiable everywhere except, perhaps, on a non-dense subset of . The following inductive argument demonstrates that. All atomic cffs are differentiable except, perhaps, on a non-dense subset of . If a finite number of cffs are differentiable except, perhaps, on non-dense subsets of , then their composition is differentiable except, perhaps, on non-dense subsets of . (For instance, the function is differentiable on , except for the non-dense set .) Thus has no cff.
The above observation, together with Lemma 2, completes the proof of Theorem 7.
11. Proof of Theorem 4,
Section 6
In this Section, I provide an analytic proof of experimentally derived Theorem 4,
Section 6. This result, re-stated as Theorem 8 below, allows for practically efficient computations of values of the continuous blancmange function for reals with finite binary floating-point representations. I also provide some properties (Lemmas 3–5) of the
Zigzag function, given by equality (
8) and visualized in
Figure 5, that are useful for a neat derivation of a formula for the blancmange function as the limit of a finite sum of some values of the
Zigzag function.
Let the function
(known as the blancmange function), visualized in
Figure 13, be defined by (
27) and
, given by (
2) and (
4), be the least number of comparisons of keys that
performs while sorting an
n-element array.
Theorem 8. (Same as Theorem 4.)
For every positive integer n and integer k with , The remainder of this section constitutes a proof of Theorem 8.
Note. The function
Zigzag, visualized in
Figure 5, has been defined by Equation (
8).
Lemma 3. For every , Proof. Let
, or
, that is,
We have
[by (
64)]
or
Additionally,
[by (
64)]
or
Now,
[by (
8)]
[by (
65) and (
66)]
[since by (
64),
]
Hence, (
63) holds. □
Lemma 4. For every , Proof. By induction on k.
Basis step:
.
Hence,
. This completes the
basis step.
Inductive step: .
Inductive hypothesis: (
67).
[by the
inductive hypothesis and by equality (
63) of Lemma 3]
Thus
This completes the
inductive step. □
Lemma 5. For every , Proof. By definition (
27) of the function
, I get
[since, for every integer
x,
, so that, for
,
]
[putting
]
which completes the proof of (
68). □
At this point, I am ready to conclude the proof of Theorem 4.
By virtue of (
15), e have
[by Lemmas 4 and 5]
that is,
from which (
63) follows.
This completes the proof of Theorem 4.
Note. A glance at the proof of Lemma 4 suffices to notice that it fails if
, and so does Theorem 4. In particular, for
, Lemma 1 yields
since, for
,
12. Final Remarks
It appears that a lack of sufficient interaction between research related to the sum of digits problem and analysis of recursive algorithms had a detrimental impact on the progress of the said research. In particular, I found that some clever use of recursion trees that are often utilized in the analysis of algorithms but are relatively rarely used in pure mathematics allowed for a relatively easy derivation of a finitary Formula (
7) that, due to its finiteness, is significantly simpler and more practical to compute (but still requiring the addition of
terms) than an instance for
of the infinite formula (1.1) derived in [
4] and quoted in
Section 8. I suppose that if the mathematicians interested in the sum of digits problem knew Formula (
7), then some of them would simplify it to Formula (
15) of the Main Theorem that can be evaluated in
for some constant
c.
A similar lack of communication between researchers has been hypothesized in [
6]: “When reading the literature on the subject we have noted that the most recent papers do not always cite more ancient ones, confirming a remark of Stolarsky […]:
Whatever its mathematical virtues, the literature on sums of digital sums reflects a lack of communication between researchers”.
On the other hand, those willing to find or quote from the literature a formula for the exact number of comparisons of keys performed by in the best case on an n-element array might have been discouraged by finding Trollope’s infinite formula (1.1) that, for , yields the count of 1s occurring in binary representations of all integers between 1 and n and has been proved, around 1974, to yield the said number of comps. To make things look even more discouraging, directly counting the said 1s requires time that is about as long as running on an increasingly sorted array of consecutive numbers from 1 to n and counting how many comps have been done. These could have been factors that stopped some of those analyzing from trying to precisely characterize its best-case time complexity, thus leaving a significant loophole in many computer science faculty and students’ knowledge of the time complexity of .
Fortunately, this article fills this important loophole.