Conceptintro
Linearithmic time
Also called n log n time, quasilinear time.
Running time of order n log n, the typical cost of efficient comparison sorting.
An algorithm runs in linearithmic time when its running time is for an input of size , or when only an upper bound is known. It sits between linear time and quadratic time: grows faster than , but more slowly than for each constant . It is the typical cost of sorting by comparisons and of divide and conquer algorithms that do linear work to split and combine.
In plain words
Sort a deck of cards by splitting it into two halves, sorting each half, and then merging the two sorted halves. The merging touches every card once. You do this at every level of the splitting. The deck can be halved about as many times as the logarithm of its size. So you touch every card once for each level, and the total work is the number of cards times the number of levels.
This is more than a single pass, but not by much. For a million items the number of levels is about 20. A method that compares every pair of items would need on the order of half a million times a million steps, and the halving method needs about twenty million.
The same shape appears when each item needs a logarithmic amount of work.
The idea
Definition
Fix a model and a case, and let be the cost. The algorithm runs in linearithmic time when . The phrase "runs in time" is the common upper-bound form, and it also holds for a linear-time algorithm. The base of the logarithm does not matter, see logarithms. The bound makes sense for .
Most authors use the name "quasilinear" for the wider class for a constant , which also contains . A few use it for alone. A page or a paper that uses the word should say which meaning it has.
Shapes of linearithmic-time algorithms
Divide and conquer with linear combining. Split the input into two parts of about half size, solve each part, and combine in linear time. The cost satisfies . By case 2 of the master theorem, . A direct count for merge sort with gives an upper bound. The bound is tight: on an input where each merge interleaves its two halves completely, each merge of items makes exactly comparisons, so equality holds at each level. Let be the largest number of comparisons. Merging two sorted lists of items each takes at most comparisons, so and . We claim for each . For both sides are . For , with the claim true for :
So the claim is true for . For the bound is comparisons. Heapsort also runs in in the worst case. Quicksort runs in in expectation with a random pivot, but its worst case is .
Linear work on each of a logarithmic number of levels. A recursion tree with levels, where the nodes of each level share a total cost of , gives . The fast Fourier transform on points uses arithmetic operations in this way.
A logarithmic operation for each item. operations on a balanced search tree or a binary heap, each of cost , cost in total. Tree sort with a balanced tree is of this kind. For a size that grows from to the sum is , and , see asymptotic notation and Stirling's approximation.
Sort first, then scan. Many tasks are solved by a sort of cost followed by a linear pass: find duplicates, group equal keys, find the closest pair on a line. The sort dominates, and the whole algorithm is linearithmic.
The lower bound for comparison sorting
Every sorting algorithm that gets its information only from comparisons of two keys needs comparisons in the worst case. Its decision tree must have at least leaves, one for each order of the input, so its height is at least . For this is , and for it is against . Since , the bound is . See comparison sort lower bound. So merge sort and heapsort are worst-case optimal in the comparison model, up to a constant factor. This bound is about the comparison model only: radix sort and counting sort use the values or digits of the keys as array indices, and they can run in linear time for integer keys of bounded range.
When the input doubles
For the exact function we have . For this is about , and for it is about . So the ratio is a little above 2 and tends to 2 from above when grows. If , this holds in the limit. An experiment with the doubling hypothesis sees a ratio near 2 and cannot tell this class from linear time without a fit that includes the logarithm. For the factor , and for it is about 30, so a linearithmic algorithm is only a small factor slower than a linear one with the same constant.
Pitfalls
- Name the case. Quicksort is in expectation and in the worst case. Merge sort is in each case. Heapsort is in the worst case, and in the best case when the keys are distinct. When all keys are equal, each sift-down stops at once, and heapsort runs in time. See worst-case analysis.
- The comparison model. The bound does not apply to algorithms that use more than comparisons, for example radix sort.
- Cost of a comparison. The bound counts comparisons or steps. When a comparison of two keys costs more than (strings of length ), the total is larger, for example character comparisons for merge sort on strings.
- Recurrence with the wrong split. The recurrence , from a split of one item against the rest, gives . The split does not need to be exact. If each part has at most a fraction of the items for a constant , for example against , the cost is still .
- Hidden extra work. A sort followed by a step of larger cost, for example a quadratic comparison of the results, makes the whole algorithm cost more than .
- Constants. A linearithmic algorithm with a large constant can be slower than a quadratic one with a small constant for every input of practical size. Insertion sort is often used for short subarrays inside sorts of this class.
Examples
- Merge sort of items: at most comparisons by the bound above.
- Any comparison sort of items: at least comparisons in the worst case.
- Heapsort of items: insertions or one build, and then extractions of the maximum with cost each, for in total.
- Check for duplicates in an array of numbers by sorting and a scan of neighbors: in the comparison model. This element distinctness problem needs steps even in the algebraic decision tree model. Ben-Or (1983) proved the bound for algebraic computation trees, a model that contains algebraic decision trees, so sort-then-scan is optimal there.
- Non-example: building a binary heap from items by the bottom-up method takes time, not , although each sift-down is . The cost of a sift-down is proportional to the height of its node, and most nodes are near the leaves.
- Non-example: counting sort of integers in the range to takes time.
- Non-example: selection sort takes comparisons in each case.
Where it is used
Linearithmic time is the standard cost of general-purpose sorting, and a sort is used as a step in many algorithms. It is also the cost of the FFT and of many divide and conquer algorithms in computational geometry. A running time below is looked for with more structure on the input. See the table of the common growth rates and the class of polylogarithmic bounds.
References
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, fourth edition, Chapter 2 (merge sort), Chapter 4 (divide and conquer) and Section 8.1 (lower bounds for sorting)
- Donald E. Knuth, The Art of Computer Programming, Volume 3, second edition, Section 5.3.1 (minimum-comparison sorting)
- Robert Sedgewick and Kevin Wayne, Algorithms, fourth edition, Section 2.2 (mergesort) and Section 1.4
- Michael Ben-Or, Lower bounds for algebraic computation trees, STOC 1983, the lower bound for element distinctness
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag EWMRWP Edit this page History Report an error Markdown JSON