Algorithms · অ্যালগরিদম
Standard algorithms for sorting, searching, graphs and optimisation, with their time/space complexity.
- Past MCQs
- 104
- Written
- 153
- Chapters
- 8
How Algorithms is asked · পরীক্ষায় যেভাবে আসে
Our 138 papers that ask Algorithms (2013–2026) hold 257 of its questions; Sorting alone makes up 31.1% of them. It makes up 10.2% of the questions in our M.Sc. admission (CSE and ICT) papers, against 1% in ICT teaching posts (NTRCA). 5 of its MCQs came back in a second exam.
- Papers that ask it
- 138
- Years covered
- 2013–2026
- MCQ / written
- 104 / 153
- MCQs asked again
- 5
Most asked chapters · বেশি আসা অধ্যায়
Each chapter's share of the 257 past-exam questions
Share of each exam's questions · পরীক্ষাভিত্তিক অংশ
Algorithms questions in each exam's papers, and their share of that exam's questions
- M.Sc. admission (CSE and ICT) of 1771810.2%
- BPSC non-cadre IT posts of 716354.9%
- BCS (ICT and CSE cadre) of 492234.7%
- Government IT officer posts of 2,337923.9%
- Bank IT jobs of 2,388863.6%
- ICT teaching posts (NTRCA) of 30031.0%
Asked in more than one exam · একাধিক পরীক্ষায় এসেছে
5 MCQs in Algorithms came back word for word in a second exam; the 3 most repeated technical ones below, each linked to the papers.
The step-by-step instructions that solve a problem are called:
Algorithms, asked in 3 exams:BREB Assistant Junior Engineer (IT) 2019Combined Bank Maintenance Engineer 2018Sonali Bank Assistant Engineer (IT) 2016
The complexity of Bubble sort algorithm is-
Algorithms, asked in 3 exams:PKB Assistant Programmer 2019Combined Bank Maintenance Engineer 2018Sonali Bank Assistant Engineer (IT) 2016
Which of the following sorting algorithms can be used to sort a random linked list with minimum time complexity?
Algorithms, asked in 2 exams:Combined Bank Officer (IT) Oct 2024Combined Bank Assistant Programmer Feb 2024
Counted from our own past papers: every past-exam MCQ and written question, each filed under one syllabus chapter. Practice questions and questions held for live tests are not counted; no answer is shown here.
Syllabus by chapter · অধ্যায়ভিত্তিক সিলেবাস
- Sorting · 185 questions
- Bubble Sort, Merge Sort; Feasible/Optimal Solution; Dijkstra's Shortest Path, Quicksort Complexity Analysis, Merge Sort — Worked Example and Complexity, Deriving the Merge Sort Recurrence T(n) = 2T(n/2) + n, Divide and Conquer Sorting (Merge Sort / Quick Sort)
- Searching · 110 questions
- Binary Search, Minimax Algorithm, Terminal State & Utility, Binary Search Using a Recursive Function, DFS vs BFS, DFS Limitations & Fixes, Binary Search — C/C++ Implementation
- Greedy & Divide and Conquer · 82 questions
- Huffman Coding, Kruskal's Algorithm, Block (Partitioned) Matrix Multiplication, Activity Selection (Maximum Non-Overlapping Intervals), Dynamic Programming vs. Divide and Conquer, Divide and Conquer vs Dynamic Programming
- Dynamic Programming · 51 questions
- Maximum Subarray Sum (Kadane's Algorithm), Dynamic Programming Explained with an Example, Fibonacci Number via Dynamic Programming, Time and Space Complexity of DP Fibonacci, Nested-Loop Complexity & Minimum Coin Change (DP)
- Graph Algorithms · 96 questions
- Dijkstra's Shortest Path Algorithm, Shortest Path (Dijkstra's Algorithm), Bellman-Ford & Negative-Weight Cycle Detection, DFS Time Complexity, Dijkstra's Algorithm, Tie-breaking by Alphabetical Order in Traversal
- Complexity & Analysis · 86 questions
- Polynomial-Time (Karp) Reduction, Recursive Function for F(n) = 1+2+...+n and Its Recurrence Relation, Big-O, Big-Omega and Big-Theta, Computing Algorithmic Complexity from Code, Fibonacci Sequence — Recursive vs Iterative Complexity, Nested Loop Output & Complexity Analysis
- Mixed Topics · 65 questions
- Regex for Odd Number of 1's, GCD Algorithm & Flowchart, Equilibrium Index of an Array, Regex for binary strings containing both 00 and 11, Generating Binary Strings without Consecutive 1s, Finding Odd Numbers from 1 to N
- Theory of Computation & Compilers · 97 questions
- DFA / NFA and regular expressions, Context-free grammars, PDA, Turing machines, Compiler design: lexical analysis, parsing, code generation, Finite state machines: Mealy / Moore, state minimisation
Loading sample questions…
Start learning: Asymptotic Analysis and Recurrences · অ্যাসিম্পটোটিক বিশ্লেষণ ও রিকারেন্স
Asymptotic Analysis and Recurrences
A written paper gives you one line: T(n) = T(n/3) + T(2n/3) + n. "Find the order of growth." Two questions further down it says: "Arrange these functions from slowest to fastest: n², 2ⁿ, n!, n^(log n), √n, n log n" and a few more. You already know how to count the loops of a program and name its class, and in Data Structures you met the simple form of the Master Theorem. Neither question yields to that. In the first, the two sub-problems have different sizes, so the simple form does not fit. In the second, no loop is given at all: you have to decide, from the functions alone, which one wins in the end, and some of those functions are not on any ladder you have memorised.
Data Structures taught you what a search costs in a list, a tree, a heap or a hash table, and every lesson there ended with a figure such as O(log n). This subject is about the algorithms that run on those structures, and almost all of them are recursive: merge sort, quicksort, binary search, Tower of Hanoi, Fibonacci, the tree walks. The cost of a recursive algorithm is itself written recursively, as a recurrence, and solving it is a skill of its own. This chapter is that skill, and it is the foundation of every algorithm lesson that follows.
By the end you will be able to rank any pile of functions with a limit test and with logarithms, and say how far "eventually" really is. You will know which rules the notations obey and which "rules" are traps. You will say best, worst and average case honestly, with a stated model of what average means. You will read a recursive function and write its recurrence, solve it by unrolling, by a recursion tree and by induction, and finish with the full Master Theorem, its extended form, and the situations where it does not apply and what to do instead.
There are nine parts, from easy to hard: comparing functions, the rules of the notations, the three cases, writing a recurrence from code, unrolling, recursion trees, substitution, the Master Theorem, and finally what lies beyond it. Every number in this chapter was computed, and every C program was compiled and run.
:::why ids=659-21,650-7,074-37,062-36,073-18,111-10,037-61,111-8,667-14,022-3,wo00337,wo00386,wo00628,wo01421
Exams come at this chapter from two sides. The MCQs ask which function grows fastest, what Θ really claims, what the average number of comparisons of a search is, what quicksort costs on average and in the worst case, and what merge sort costs (BCS written, 2025; Sonali, Janata and Rupali Bank, 2021). The written papers ask you to build a recurrence from a recursive function (Milk Vita, 2023), to solve one by hand (BPSC, 2021: T(n) = 3T(n − 1) + 2), or to find the complexity of a problem split into eight halves with an O(n²) combine (Microcredit Regulatory Authority, 2023). Each of these ideas is taught below, with its reason.
:::
:::hook
Using the Master Theorem, the recurrence T(n) = 4T(n/2) + n² has the solution:
- Θ(n²)
- Θ(n² log n)
- Θ(n³)
- Θ(n log n)
Θ(n² log n). Read off a = 4 and b = 2, so the critical exponent is log₂ 4 = 2. The work outside the calls is f(n) = n², which has exactly the same exponent. When the two are equal, every level of the recursion tree costs the same, n², and there are log₂ n levels: n² × log n. We ran the recurrence for n = 2⁶⁰ and the ratio T(n) / (n² log₂ n) was 1.0167, settling near 1. Θ(n²) forgets the levels, Θ(n³) is the answer when the leaves win, and Θ(n log n) does not even grow as fast as the work in the top call. Part 8 builds this rule from the tree.
:::
Part 1: Comparing functions: the limit test and the log trick
When the ladder is not enough
In Data Structures you climbed a ladder: 1, log n, √n, n, n log n, n², n³, 2ⁿ, n!. You also learned what the five signs mean: O is "at most", Ω is "at least", Θ is "exactly the same order", and the small o and ω are the strict versions, "strictly slower" and "strictly faster". Formally, f(n) = O(g(n)) means that f(n) ≤ c·g(n) for some constant c and every n from some n₀ on; everything in this chapter rests on that one sentence.
The ladder is a good start, but it has gaps. Where do n^(log n), 2^√n or (log n)^(log n) go? Is n! above or below nⁿ? Is 2ⁿ the same order as 3ⁿ? These are not exotic: they turn up in every "arrange in increasing order" question, and a ladder cannot answer them. We need two tools that work on any pair of functions. The first is a limit, and the second is the logarithm.
The limit test
Picture two runners on a long road. At every n you look at the ratio of their distances, f(n) / g(n). If the ratio shrinks to zero, f is the slow runner: f = o(g). If it settles at some positive finite number, they run at the same pace: f = Θ(g). If it grows without bound, f is far ahead: f = ω(g). That is the whole test. Compute the limit of f(n) / g(n) as n grows, and read the answer from three outcomes.
:::table caption="The limit test for f(n) / g(n) as n grows" wrap
| The ratio f(n) / g(n) tends to | What it tells you | Also true |
|---|---|---|
| 0 | f = o(g): f grows strictly slower | f = O(g), and f is not Ω(g) |
| a positive constant | f = Θ(g): the same order | f = O(g) and f = Ω(g) |
| ∞ | f = ω(g): f grows strictly faster | f = Ω(g), and f is not O(g) |
| ::: |
For polynomials the test is almost instant: (3n² + 5n) / n² tends to 3, a positive constant, so 3n² + 5n = Θ(n²). When the functions are mixed, you use the rule you know from calculus: if both go to infinity, the limit of the ratio equals the limit of the ratio of the derivatives (L'Hôpital's rule). Is log n smaller than √n? The ratio is ln n / √n, and the derivatives are (1/n) / (1/(2√n)) = 2/√n, which goes to 0. So log n = o(√n). Repeat the argument and you get the general fact: any power of log n is o(nᵋ) for every ε > 0, however small. Logarithms lose to every root.
Now a warning that costs marks. "Loses in the end" does not mean "loses at the sizes you can see". Here is (log₂ n)³ divided by √n, computed:
n = 10³ (log₂ n)³ / √n = 31.30
n = 10⁶ (log₂ n)³ / √n = 7.92
n = 10⁹ (log₂ n)³ / √n = 0.85
The ratio rises, peaks and then falls toward 0. The cube of the logarithm stays above √n for every n up to about 6 × 10⁸. With a tenth power, (log₂ n)¹⁰ stays above n itself until n is about 5 × 10¹⁷. So the claim "log n beats any power" is true, but "eventually" can be a very long way off, and the exam question means the true, limiting order. Never decide by plugging in n = 100.
The exams ask exactly this. The 49th BCS Written (2025, 659-21) asks which of O(log n), O(n), O(n log n) and O(√n) runs fastest: at n = 10⁶ the values are about 20, 1,000, a million and twenty million, so O(log n) is fastest (log₂ n and √n tie at n = 16, and from n = 17 on log n is below √n). The 47th BCS Preliminary (2025, 650-7) gives A = O(n²) and B = O(n), and the safe answer is "A is asymptotically slower", because Big-O speaks only about large n. For small n it can be reversed: an algorithm taking n² steps beats one taking 100n steps until n = 100 and loses to it from there on.
In one line: compute f(n) / g(n) as n grows: 0 means f is strictly smaller, a constant means the same order, ∞ means strictly larger.
The log trick
Limits get clumsy when the functions are powers of powers, such as n^(log n) against 2ⁿ. Here a different trick works: take the logarithm of both. Since f / g = e^(ln f − ln g), the ratio tends to 0 exactly when ln f − ln g tends to −∞, and to a positive constant exactly when the difference stays bounded. Notice what we compare: the difference of the logs, not the logs themselves. Division has become subtraction.
:::table caption="Pairs settled by comparing logarithms" wrap
| Pair | Difference of logs | Verdict |
|---|---|---|
| n² against n³ | ln n² − ln n³ = −ln n, which tends to −∞ | n² = o(n³) |
| 2n² against n² | ln 2, a constant | 2n² = Θ(n²) |
| 2^(n+1) against 2ⁿ | ln 2, a constant | 2^(n+1) = Θ(2ⁿ) |
| 2^(2n) against 2ⁿ | n ln 2, which tends to ∞ | 2^(2n) = ω(2ⁿ), so 4ⁿ is not O(2ⁿ) |
| n^(1/log₂ n) against 1 | log₂ of it is (1/log₂ n) · log₂ n = 1 | it equals 2 for every n: Θ(1) |
| ::: |
Look at the last row. n^(1/log₂ n) looks as if it should shrink or grow, and it is a constant: for n = 4, 16, 1,024 and 10⁶ the program printed 2.0 every time. The log trick sees it at once.
Now the pair that started the chapter. Which is bigger in the end, n^(log n) or 2ⁿ?
:::steps title="Compare n^(log n) with 2ⁿ by taking logarithms"
Write m = log₂ n, so n = 2ᵐ. All logarithms below are to base 2.
- Log of the first function. log₂ (n^(log₂ n)) = (log₂ n) · (log₂ n) = (log₂ n)² = m².
- Log of the second. log₂ (2ⁿ) = n = 2ᵐ.
- Compare m² with 2ᵐ. This is "a power of the variable against an exponential of it", and the exponential wins: 2ᵐ eventually dominates m². So log (2ⁿ) beats log (n^(log n)) by more and more, and 2ⁿ is the bigger function in the end.
- Find where the change happens. m² = 2ᵐ exactly at m = 2 and m = 4, which is n = 4 and n = 16. At n = 4 both are 16. At n = 8, n^(log n) = 8³ = 512 against 2⁸ = 256, so n^(log n) is bigger. At n = 16 both are 2¹⁶ = 65,536 again, and from n = 17 on, 2ⁿ is bigger for ever.
So n^(log n) = o(2ⁿ). The same step shows that n^(log n) is bigger than every polynomial nᵏ: its log m² beats k·m once m exceeds k. It is called superpolynomial but subexponential, and it sits in the gap between the two rungs of the ladder.
:::
:::figure caption="Taking logs turns each function into a simple curve: 2ⁿ becomes a straight line, n^(log n) becomes (log₂ n)², and n³ becomes 3 log₂ n. The solid and the dashed amber curves meet at n = 4 and n = 16." alt="A graph with n from 0 to 40 along the bottom and log base 2 of f of n up the side. A solid amber straight line shows 2 to the n. A dashed amber curve shows n to the power log n; it meets the line at n equal 4 and n equal 16, is above it between them, and below it afterwards. A grey flat curve shows n cubed and ends lowest." maxw=440
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 360 236" width="360" height="236"><text x="4" y="13" text-anchor="start" font-size="12" font-weight="700" class="f-ink">Taking logs: height = log<tspan dy="3" font-size="8">2</tspan><tspan dy="-3"> f(n)</tspan></text><path class="f-line" d="M40 18V196H350"/><path class="f-line" d="M37 196H40"/><text x="34" y="199.5" text-anchor="end" font-size="10.5" class="f-mute">0</text><path class="f-line" d="M37 152.5H40"/><text x="34" y="156" text-anchor="end" font-size="10.5" class="f-mute">10</text><path class="f-line" d="M37 109H40"/><text x="34" y="112.5" text-anchor="end" font-size="10.5" class="f-mute">20</text><path class="f-line" d="M37 65.5H40"/><text x="34" y="69" text-anchor="end" font-size="10.5" class="f-mute">30</text><path class="f-line" d="M37 22H40"/><text x="34" y="25.5" text-anchor="end" font-size="10.5" class="f-mute">40</text><path class="f-line" d="M40 196V199"/><text x="40" y="211" text-anchor="middle" font-size="10.5" class="f-mute">0</text><path class="f-line" d="M116.5 196V199"/><text x="116.5" y="211" text-anchor="middle" font-size="10.5" class="f-mute">10</text><path class="f-line" d="M193 196V199"/><text x="193" y="211" text-anchor="middle" font-size="10.5" class="f-mute">20</text><path class="f-line" d="M269.5 196V199"/><text x="269.5" y="211" text-anchor="middle" font-size="10.5" class="f-mute">30</text><path class="f-line" d="M346 196V199"/><text x="346" y="211" text-anchor="middle" font-size="10.5" class="f-mute">40</text><text x="350" y="225" text-anchor="end" font-size="11.5" font-weight="700" class="f-ink">n</text><polyline class="f-line" points="47.6,196 51.5,188.4 55.3,182.9 59.1,178.7 63,175.3 66.8,172.4 70.6,169.9 74.4,167.7 78.2,165.7 82.1,163.9 85.9,162.3 89.7,160.8 93.5,159.4 97.4,158.1 101.2,156.8 105,155.7 108.8,154.6 112.7,153.6 116.5,152.6 120.3,151.7 124.2,150.9 128,150 131.8,149.2 135.6,148.4 139.4,147.7 143.3,147 147.1,146.3 150.9,145.7 154.8,145 158.6,144.4 162.4,143.8 166.2,143.2 170.1,142.7 173.9,142.1 177.7,141.6 181.5,141.1 185.3,140.6 189.2,140.1 193,139.6 196.8,139.1 200.7,138.7 204.5,138.2 208.3,137.8 212.1,137.4 215.9,137 219.8,136.6 223.6,136.2 227.4,135.8 231.2,135.4 235.1,135 238.9,134.7 242.7,134.3 246.6,133.9 250.4,133.6 254.2,133.3 258,132.9 261.9,132.6 265.7,132.3 269.5,132 273.3,131.7 277.1,131.3 281,131 284.8,130.8 288.6,130.5 292.4,130.2 296.3,129.9 300.1,129.6 303.9,129.3 307.8,129.1 311.6,128.8 315.4,128.5 319.2,128.3 323.1,128 326.9,127.8 330.7,127.5 334.5,127.3 338.4,127 342.2,126.8 346,126.5"/><polyline class="f-hostline" stroke-dasharray="5 3" points="47.6,196 51.5,194.5 55.3,191.7 59.1,188.4 63,185.1 66.8,181.8 70.6,178.6 74.4,175.5 78.2,172.5 82.1,169.7 85.9,166.9 89.7,164.3 93.5,161.7 97.4,159.2 101.2,156.8 105,154.5 108.8,152.3 112.7,150.1 116.5,148 120.3,145.9 124.2,143.9 128,142 131.8,140.1 135.6,138.2 139.4,136.4 143.3,134.7 147.1,132.9 150.9,131.3 154.8,129.6 158.6,128 162.4,126.4 166.2,124.8 170.1,123.3 173.9,121.8 177.7,120.4 181.5,118.9 185.3,117.5 189.2,116.1 193,114.7 196.8,113.4 200.7,112.1 204.5,110.8 208.3,109.5 212.1,108.2 215.9,107 219.8,105.8 223.6,104.6 227.4,103.4 231.2,102.2 235.1,101 238.9,99.9 242.7,98.8 246.6,97.7 250.4,96.6 254.2,95.5 258,94.4 261.9,93.3 265.7,92.3 269.5,91.3 273.3,90.2 277.1,89.2 281,88.2 284.8,87.2 288.6,86.3 292.4,85.3 296.3,84.4 300.1,83.4 303.9,82.5 307.8,81.6 311.6,80.6 315.4,79.7 319.2,78.8 323.1,77.9 326.9,77.1 330.7,76.2 334.5,75.3 338.4,74.5 342.2,73.6 346,72.8"/><polyline class="f-hostline" points="47.6,191.7 51.5,189.5 55.3,187.3 59.1,185.1 63,182.9 66.8,180.8 70.6,178.6 74.4,176.4 78.2,174.2 82.1,172.1 85.9,169.9 89.7,167.7 93.5,165.6 97.4,163.4 101.2,161.2 105,159 108.8,156.8 112.7,154.7 116.5,152.5 120.3,150.3 124.2,148.2 128,146 131.8,143.8 135.6,141.6 139.4,139.4 143.3,137.3 147.1,135.1 150.9,132.9 154.8,130.8 158.6,128.6 162.4,126.4 166.2,124.2 170.1,122 173.9,119.9 177.7,117.7 181.5,115.5 185.3,113.3 189.2,111.2 193,109 196.8,106.8 200.7,104.7 204.5,102.5 208.3,100.3 212.1,98.1 215.9,96 219.8,93.8 223.6,91.6 227.4,89.4 231.2,87.2 235.1,85.1 238.9,82.9 242.7,80.7 246.6,78.5 250.4,76.4 254.2,74.2 258,72 261.9,69.8 265.7,67.7 269.5,65.5 273.3,63.3 277.1,61.2 281,59 284.8,56.8 288.6,54.6 292.4,52.4 296.3,50.3 300.1,48.1 303.9,45.9 307.8,43.8 311.6,41.6 315.4,39.4 319.2,37.2 323.1,35.1 326.9,32.9 330.7,30.7 334.5,28.5 338.4,26.3 342.2,24.2 346,22"/><circle cx="70.6" cy="178.6" r="3.6" class="f-net"/><text x="70.6" y="194.6" text-anchor="middle" font-size="10.5" font-weight="700" class="f-ink">n = 4</text><circle cx="162.4" cy="126.4" r="3.6" class="f-net"/><text x="162.4" y="142.4" text-anchor="middle" font-size="10.5" font-weight="700" class="f-ink">n = 16</text><text x="319.2" y="32.2" text-anchor="end" font-size="11.5" font-weight="700" class="f-ink">2ⁿ</text><text x="346" y="66.9" text-anchor="end" font-size="11.5" font-weight="700" class="f-ink">n^(log n)</text><text x="346" y="140.5" text-anchor="end" font-size="11.5" font-weight="700" class="f-ink">n³</text><text x="46" y="230" text-anchor="start" font-size="10.5" class="f-mute">between n = 4 and n = 16 the dashed curve is higher</text></svg>
:::
The hard rungs between polynomial and exponential
Two more functions live in that gap, and they trip people up. The first is (log n)^(log n). Take logs and it is log n · log log n; take logs of n^(log log n) and you get the same product. So (log n)^(log n) = n^(log log n) exactly. Since log log n tends to infinity, this function is above every polynomial, and since log log n is smaller than log n, it is below n^(log n). The second is 2^√n. Its log is √n, which beats (log n)² once n passes 65,536 (at n = 2¹⁶ both equal 256). So in the end 2^√n is above n^(log n) but still far below 2ⁿ.
All of this is about the end, and the table below shows how different the middle can look. It gives log₂ f(n) for four functions at three sizes. The values are computed, and the order is not the same in all three rows.
:::table caption="log₂ of the function at three sizes: the order you see depends on how far you look" wrap
| n | n³ | 2^√n | (log n)^(log n) | n^(log n) |
|---|---|---|---|---|
| 2¹⁰ = 1,024 | 30 | 32 | 33.2 | 100 |
| 2²⁰ ≈ 10⁶ | 60 | 1,024 | 86.4 | 400 |
| 2⁶⁴ | 192 | 4.3 × 10⁹ | 384 | 4,096 |
| ::: |
At n = 1,024 the order is n³ < 2^√n < (log n)^(log n) < n^(log n). At n = 2²⁰ it is (log n)^(log n) < n^(log n) < 2^√n, and 2^√n has jumped over two others. The limiting order, the one an exam wants, is the second pattern: n³ < (log n)^(log n) < n^(log n) < 2^√n.
Factorials and Stirling
n! multiplies n numbers, 1 × 2 × … × n, and 2ⁿ multiplies n twos, so n! looks bigger. It is bigger, from n = 4 on: 4! = 24 against 2⁴ = 16, and every step multiplies n! by n + 1 ≥ 5 but 2ⁿ only by 2, so the gap widens for ever. Also n! < nⁿ, because nⁿ multiplies n copies of n.
A closer fit is Stirling's approximation, n! ≈ √(2πn) · (n/e)ⁿ, where e = 2.71828. The ratio of n! to the approximation tends to 1, and is already close for small n:
n n! n! / Stirling's value
5 120 1.0168
10 3,628,800 1.0084
20 2.43 × 10¹⁸ 1.0042
50 3.04 × 10⁶⁴ 1.0017
100 9.33 × 10¹⁵⁷ 1.0008
Stirling also tells us that n! is far below nⁿ: nⁿ / n! is about eⁿ / √(2πn), which is 2.9 × 10²⁰ at n = 50. And we get the size of log (n!) without it. Since n! is at most nⁿ and at least (n/2)^(n/2) (the top half of the factors are each at least n/2), taking log₂ gives (n/2) log₂(n/2) ≤ log₂(n!) ≤ n log₂ n. Both ends are of order n log n, so log (n!) = Θ(n log n). For n = 1,000 the three numbers are 4,483, 8,529 and 9,966. This fact is why comparing n items needs about n log n steps in the best sorting methods, and you will use it again.
The whole ladder, in exam form
Here is the full order, with the hard rungs in place. For each neighbouring pair the right-hand column gives the one-line reason, which is the thing an examiner wants to see.
:::table caption="From slowest to fastest, with the reason each function is above the one before" wrap
| Function | Why it is above the one before |
|---|---|
| 1 | a constant |
| log log n | grows, but only like the log of a log |
| log n | log log n is the log of log n, a smaller number |
| (log n)² | a power above 1 of the same quantity |
| √n | any power of log is o(nᵋ) |
| n | √n = n^(1/2) is a smaller power of n |
| n log n | n × log n, with log n tending to ∞ |
| n² | n log n / n² = log n / n tends to 0 |
| n³ | n² / n³ tends to 0 |
| (log n)^(log n) | equals n^(log log n), and log log n beats every constant exponent |
| n^(log n) | the exponent log n beats log log n |
| 2^√n | log is √n against (log n)² |
| 2ⁿ | √n < n in the exponent |
| 3ⁿ | 3ⁿ / 2ⁿ = (3/2)ⁿ tends to ∞: the base inside an exponent matters |
| n! | n! beats cⁿ for every constant c |
| nⁿ | n! < nⁿ, and the ratio is about eⁿ |
| 2^(n²) | log₂ is n² against n log n |
| ::: |
Never plug in a convenient n, because the order is about the limit; and when two functions look alike, take logs and look at the difference.
In one line: to rank functions, take the limit of the ratio or compare the difference of their logs, and remember that "wins in the end" can be astronomically far away.
:::check
Which statement is true as n grows without bound?
- n^(log₂ n) = O(n¹⁰⁰)
- 2ⁿ = O(n^(log₂ n))
- n^(log₂ n) = o(2ⁿ)
- n! = O(2ⁿ)
Take log₂ of each side. For the third statement we compare (log₂ n)² with n, and n wins for every n > 16, so n^(log n) = o(2ⁿ). The first fails because (log₂ n)² exceeds 100 log₂ n as soon as log₂ n > 100: n^(log n) beats every fixed power. The second is the reverse of the true statement. The fourth is false because n! overtakes 2ⁿ at n = 4 and keeps growing faster.
:::
Read the whole lesson free with an account · পুরো পাঠ পড়তে বিনামূল্যে সাইন আপ করুন
All 15 lessons · সব পাঠ
- Asymptotic Analysis and Recurrences · অ্যাসিম্পটোটিক বিশ্লেষণ ও রিকারেন্স
- Sorting Algorithms · সর্টিং অ্যালগরিদম
- Searching and Divide-and-Conquer · সার্চিং এবং ডিভাইড-অ্যান্ড-কনকার
- Greedy Algorithms · গ্রিডি অ্যালগরিদম
- Dynamic Programming · ডাইনামিক প্রোগ্রামিং
- Graph Algorithms: Shortest Paths, MST and Max Flow · গ্রাফ অ্যালগরিদম: শর্টেস্ট পাথ, MST ও ম্যাক্স-ফ্লো
- Theory of Computation I: Regular Expressions, Finite Automata and the Pumping Lemma · থিওরি অব কম্পিউটেশন ১: রেগুলার এক্সপ্রেশন, ফাইনাইট অটোমাটা ও পাম্পিং লেমা
- Theory of Computation II: Grammars, Pushdown Automata, the Chomsky Hierarchy and Turing Machines · থিওরি অব কম্পিউটেশন ২: গ্রামার, পুশডাউন অটোমাটা, চমস্কি হায়ারার্কি ও টুরিং মেশিন
- Complexity Classes: P, NP, NP-Complete and Reductions · কমপ্লেক্সিটি ক্লাস: P, NP, NP-কমপ্লিট ও রিডাকশন
- AI Search: Blind and Heuristic Search, Local Search, Games, Genetic Algorithms and CSP · এআই সার্চ: ব্লাইন্ড ও হিউরিস্টিক সার্চ, লোকাল সার্চ, গেম, জেনেটিক অ্যালগরিদম ও CSP
- Elementary Algorithms and Flowcharts · প্রাথমিক অ্যালগরিদম ও ফ্লোচার্ট
- Sorting, Searching and String Matching in C · সি-তে সর্টিং, সার্চিং ও স্ট্রিং ম্যাচিং
- Backtracking and Branch and Bound · ব্যাকট্র্যাকিং এবং ব্রাঞ্চ অ্যান্ড বাউন্ড
- Graph Algorithms II: DAG Shortest Paths, Topological Sort, SCC, Cut Property, Union-Find · গ্রাফ অ্যালগরিদম II: DAG শর্টেস্ট পাথ, টপোলজিক্যাল সর্ট, SCC, কাট প্রপার্টি, ইউনিয়ন-ফাইন্ড
- Revision sheet: Algorithms · রিভিশন শিট: অ্যালগরিদম