Merge sort Created 2026-10-13 Updated 2026-10-18
Merge sort is a divide-and-conquer algorithm that sorts a finite array by splitting it into contiguous halves, sorting each half using recursion, and combining the two sorted arrays with the merge algorithm. An empty or one-entry array is returned immediately. For length , put the first entries on the left and the remaining on the right; the brackets are the floor function and ceiling function. Always choose the left head on equal keys to obtain stable sorting. This is the split, sort, combine structure taught in the MIT lecture notes.[ref]
Take the concrete input array
The tags identify the entries originally at indices and , counting from zero. Both keys are ; the tags are never compared. Reading downward in the figure follows the splits, then the returned sorted arrays. Arrows show dependencies; the level layout groups independent calls rather than giving their chronological execution order.
Figure 1.
Merge sort splits into base cases and returns sorted arrays
. The tagged keys 2a and 2b retain their original order through stable sorting. The merge algorithm makes 4, 6 and 7 head comparisons on the three returning levels, for 17 in total. Details.
The first two halves are and . Their pairs split into one-entry base cases. Applying the merge algorithm gives the following fully worked return levels; each number in the last column counts only comparisons between two available heads.
Returning levelResulting sorted arraysHead comparisons
Pairs, , ,
Halves,
Whole array
In the left-half merge algorithm, the head pairs are , and ; it takes , then appends . In the final merge algorithm, the head pairs are , , , , , and . It takes , then appends . Thus this precise version makes head comparisons and, ignoring identity tags, returns .
The elegance of merge sort is that it creates exactly the structure its next step needs: two smaller sorted arrays make the larger ordering inexpensive. Once the whole array is sorted, equal keys are adjacent and binary search can serve repeated queries. Stable sorting also retains information that the keys alone do not express. If the left-half merge algorithm instead takes the right head on the tie, it returns ; the keys are sorted but stable sorting has failed.
For time complexity, assume each key comparison, entry access and entry copy has constant cost. The merge algorithm processes entries in time. In Big O notation, the resulting recurrence relation is
For , the balanced levels contain entries each and there are returning levels. Each costs , so the worst-case time complexity is . For arbitrary , there are at most returning levels, each processing at most entries, giving the same bound. The worst-case number of head comparisons is also . Here is the base-two logarithm. The MIT notes give this level-by-level analysis for merge sort.[ref]
The conventional array implementation uses a reusable buffer of entries for the merge algorithm, together with pending calls for recursion. Its auxiliary space complexity is therefore , beyond the original array. This bound describes that implementation. A linked list permits a merge algorithm that reconnects existing nodes; its storage accounting differs from copying array entries into a buffer.
For a short algorithm correctness outline, the empty and one-entry base cases are sorted. Assuming recursion correctly sorts both shorter halves, the merge algorithm returns all their entries in order. Equal keys within a half keep their relative order by the same argument applied to the smaller call; across halves, every left entry originally preceded every right entry, so taking left first preserves stable sorting. Both half-lengths decrease for , establishing termination.
Let and suppose the recurrence relation is defined with no zero iterates. An eventually constant sign would make satisfy on a tail. Thus , although positivity of requires throughout that tail. This contradiction proves infinitely many sign changes. The nonzero assumption matters because a zero iterate makes the next step undefined.
For and , the displayed recurrence relation stays positive and satisfies
After the first step, , so . The bounded monotone sequence theorem gives a limit of a sequence ; the inequality makes positive, and passage to the limit of a sequence in the recurrence relation gives . This proves existence of the positive square root without assuming it in the construction. Once is known, the exact error identity explains the eventual quadratic convergence.
For positive integers , the north-east lattice path recurrence is
The boundaries have only straight north-east lattice paths. The origin counts the zero-step north-east lattice path once.[ref]
Proof outline. A north-east lattice path ending at has a final step either from or from . Deleting that step gives a shorter north-east lattice path; appending it reverses the operation. The two classes are disjoint and exhaust all possibilities, so the rule of sum adds their counts. At , the arrivals from contribute , while those from contribute : altogether .
Set if either integer coordinate is negative. Then the same recurrence relation applies on the nonnegative axes away from : one predecessor contributes one, and the other contributes zero. Initialize separately; applying the recurrence relation there would incorrectly give zero.
Computing the grid once in increasing order of is dynamic programming: every entry uses two earlier entries. If a specified point is forbidden, set its count to zero, then continue the same recurrence relation at allowed points. This counts north-east lattice paths avoiding the forbidden points, provided the origin and endpoint are allowed. The last-step argument remains valid because appending a step to an allowed endpoint cannot visit any new intermediate integer lattice point.
Optimal substructure 2026-10-14
Optimal substructure means that the relevant parts of an optimal solution solve their own subproblems optimally. For a 0-1 knapsack problem, removing a chosen item leaves an optimal selection among the other allowed items at the reduced capacity. For sequential mathematical optimization, this becomes the dynamic programming principle: after fixing the next decision and resulting dynamic programming state, use an optimal continuation. Optimal substructure justifies the recurrence relation; overlap between subproblems explains why storing a value function can save work.
Overlapping subproblems 2026-10-13
Overlapping subproblems occur when several parts of a calculation request the same smaller problem. Memoization can share its answer if the memoization key identifies that problem completely. For the Fibonacci numbers, both and require . In dynamic programming, the dynamic programming state plays the corresponding role of identifying a reusable continuation. Overlap explains potential savings; the recurrence relation and its base cases still need to be correct.
Pascal's rule Created 2026-10-13 Updated 2026-10-18
Pascal's identity is
For an integer and any integer , Pascal's rule holds with the convention outside . The boundary values are , including . Initialize row zero separately: the recurrence relation does not require binomial coefficients with top index .[ref]
Proof by a marked element. Count the -element subsets of , marking the element as special. Those omitting are exactly the -element subsets of . From those containing , delete to obtain the -element subsets of that same smaller set; adjoining reverses this operation. The rule of sum adds these two disjoint counts. The proof includes and because the impossible class contributes zero.
For an original worked instance, choose three labels from and mark . Omitting it gives selections. Including it leaves two labels to choose from the other six, giving . Therefore . Under the bijection with north-east lattice paths to , membership of position seven says whether the final step is north: the marked-element split becomes the last-step split.
This makes Pascal's rule useful beyond an algebraic identity. A counting problem may have an obvious yes-or-no decision even when its factorial formula is obscure; splitting at that decision exposes a recurrence relation. When the smaller counts are shared between many cases, dynamic programming reuses them. The beauty of Pascal's triangle is that one local addition rule simultaneously organizes subsets, binary strings, north-east lattice paths, and the binomial theorem.
First classify nonzero polynomial solutions of the differential equation itself. If has degree and leading coefficient , the coefficient of is . Hence is necessary.
An analytic solution at zero belongs to the exponent-zero Frobenius branch. Its odd-power coefficients vanish, because and the recurrence relation preserves parity. For the even-power coefficients,
All denominators are nonzero for . With , the coefficients through are nonzero when , the next coefficient vanishes, and every subsequent coefficient vanishes. This proves sufficiency as well as the restriction to even polynomial degree:
For example, setting gives when , when , and when . A polynomial cannot instead begin with a positive integer power: its lowest power must have an indicial exponent, and the only integer indicial exponent is zero. This also proves that no odd-degree family was missed. The finite-series mechanism is a terminating Frobenius series.
If the additional endpoint conditions from part (b) are imposed on this part too, there is no nonzero polynomial solution: every nonzero polynomial just classified has . The displayed family and degree answer therefore concern the differential equation without those extra endpoint conditions.
Substitute a Frobenius series into the transformed equation. For , equating the coefficient of gives the recurrence relation
At the larger indicial root , the coefficient of is , so a Frobenius solution always exists. At the smaller indicial root , the first two equations are
Since , the exceptional equation is consistent exactly when
These are both admissible values, including the zero value; the singular wording of the question does not remove either. This is the undetermined coefficient at Frobenius resonance: at either value is free, whereas otherwise the second independent solution requires a Logarithmic Frobenius solution.
Explicit pairs demonstrate sufficiency and linear independence. In the variable one may take
Both displayed have leading term , while . They therefore give independent Frobenius solutions with the two indicial exponents, and direct differentiation verifies the equation. The point is an apparent regular singular point in these two cases.
For the requested closed forms, particularly short choices are
In addition, solves the zero-parameter equation and solves the equation with .
For the first Frobenius solution, take the larger indicial root and normalize the first coefficient to one:
Substitution and comparison of the coefficient of give
Every denominator is nonzero because .
For , the recurrence relation is , so
For , and all odd coefficients vanish; the even coefficients satisfy . Hence
For any positive integer , induction in the recurrence relation shows that only indices divisible by survive:
The empty product for equals one. Successive terms in the power series factor have ratio of magnitudes
Thus the ratio test gives convergence for every finite ; a nonintegral still requires a chosen side of zero or a complex branch for the factor .
For a putative second Frobenius solution with the smaller indicial root , set , with and for . Its recurrence relation is
The Frobenius case classification concerns the gap between the indicial roots. In the course convention, Case IIIa is the obstructed recurrence requiring a Logarithmic Frobenius solution; Case IIIb permits two pure Frobenius series.
If and , this is Case I: no denominator vanishes. There are two linearly independent pure Frobenius solutions, with
Their different leading powers establish linear independence.
If , this is Case II, with a repeated indicial root. There is only one pure Frobenius series with a nonzero leading coefficient. To see the second solution's form, reduction of order gives
Here , so the integrand is . Consequently
The logarithm has a nonzero coefficient and cannot be removed by adding a multiple of .
If is a positive integer, the only potentially obstructed coefficient is . Below that index, unless divides . At the exceptional index the equation reads
If for some positive integer , all coefficients are nonzero because . The exceptional equation is inconsistent. This is Case IIIa, and the independent second solution has the form
The Frobenius resonance for an Euler equation with monomial potential explains why divisibility, rather than the integer gap alone, decides this outcome. For example, with , applying the original differential operator to gives . Cancellation of the coefficient of requires .
If does not divide , then ; this includes through the negative-index convention. The exceptional equation is , so is free. This is Case IIIb: set to select an independent pure Frobenius series starting with . Subsequent coefficients again follow the recurrence relation. Changing simply adds a multiple of , the undetermined coefficient at Frobenius resonance.
The complete answer is therefore
In particular, for every positive integer gap gives Case IIIa. For , positive integer gives Case IIIa, while gives Case IIIb; the remaining positive give Case I. The condition matters: at the two Cauchy-Euler equation powers already give independent solutions whenever .
Normalize both leading coefficients to one. For the indicial root , the recurrence relation from part (b) gives
Recognizing the Taylor series of the sine gives the first Frobenius solution
For the smaller indicial root , . The equation at is ; choose the free coefficient to avoid adding the first Frobenius solution. The remaining coefficients are
Thus the Taylor series of the cosine gives
Their leading terms are and , as required for Case IIIb. A direct check uses : the differential equation becomes , so these two functions solve it on . Their Wronskian is , proving linear independence. The general solution is .
Substitute the power series and its termwise derivatives into the ordinary differential equation. The coefficient of , for , is
The bracket factors as , so the power-series solution of a differential equation satisfies
The odd and even recurrence relations terminate: , , and , , . Thus the full solution is . The two polynomials have independent initial data at zero, and the leading coefficient never vanishes on the real axis, so every real solution of a differential equation for this equation is their linear combination on the entire real axis. The given initial data select , :
Differentiating the Rodrigues representation directly gives the identity
We prove simultaneously by induction that for . It holds for , . Suppose it holds at . Differentiating the preceding identity and using the same identity at gives
The induction is complete. Substituting the derivative identity into the first formula proves the Hermite polynomial three-term recurrence relation
The initial step supplies the recurrence at without needing to define .
Be careful about the index: the probability density function already contains a factor , so . The recurrence relation at yields . Therefore linearity of expectation gives
Thus is an unbiased estimator.
The recurrence relation at gives . Consequently
By variance additivity for independent random variables, the variance of their sample mean is
Rewrite the recurrence relation as
Since and , a positive gives a positive . Mathematical induction therefore proves , and also shows that every division in the recurrence relation is defined.
The tail is a monotone sequence bounded below by zero. The bounded monotone sequence theorem therefore gives . Taking the limit of a sequence in yields , so . The reciprocal rule from part (a) permits passage to the limit of a sequence in the recurrence relation:
Multiplication by gives
This proves existence of the positive square root without assuming that existence when constructing the sequence.
Write . Suppose, for a contradiction, that only finitely many sign changes occur. Since every , the sign is then constant for . Set on that tail. Multiplying the recurrence relation by this constant sign gives
Consequently for , and by comparison with a geometric sequence. But positivity of requires at every step, which is incompatible with . Thus the negative-parameter square-root iteration changes sign infinitely often.
Only (ii) is necessarily true. Every real satisfies , so (ii) holds automatically. For a single counterexample to (i) and (iii), choose and . The recurrence relation then gives the nonzero periodic sequence
Here , disproving (i), and , disproving (iii). This counterexample satisfies the requirement that no iterate is zero.