Algorithms and Big-O complexity
An algorithm is a finite sequence of well-defined steps; Big-O notation describes how its cost grows with input size.
Edited by Alex C, EduAlx · Sources cited below
Explanation
Common classes: O(1) constant, O(log n) logarithmic, O(n) linear, O(n log n), O(n²) quadratic, O(2ⁿ) exponential. Knuth popularised asymptotic analysis in computing. Worst, average and best cases can differ.
Example
Code
def binary_search(a, x):
lo, hi = 0, len(a) - 1
while lo <= hi:
mid = (lo + hi) // 2
if a[mid] == x: return mid
if a[mid] < x: lo = mid + 1
else: hi = mid - 1
return -1
How it works, step by step
- Define the input size n
- Count basic operations as a function of n
- Keep the dominant term
- Drop constant factors
Open the interactive page with diagrams →
People behind it
- Donald Knuth (b. 1938), Computer science, USA: Stanford professor; author of The Art of Computer Programming and creator of TeX. Turing Award 1974.
- Al-Khwarizmi (c. 780–c. 850), Mathematics and astronomy, Persia / Baghdad: Scholar at the House of Wisdom; his book on al-jabr gave algebra its name, and 'algorithm' comes from his name.
- Ada Lovelace (1815–1852), Mathematics and computing, UK: Wrote notes on Babbage's Analytical Engine (1843) with what is seen as the first published algorithm for a machine.
- Charles Babbage (1791–1871), Mathematics and engineering, UK: Designed the Difference Engine and the programmable Analytical Engine.
References
- D. E. Knuth, SIGACT News 1976, 8(2), 18–24 (Big Omicron and Big Omega and Big Theta).
Textbooks
- Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein (MIT Press, 4th ed. 2022). 'CLRS', the standard algorithms text.
- The Art of Computer Programming (Vols. 1–4B), Donald E. Knuth (Addison-Wesley, 1968–2023). Monumental work on algorithms.
Related topics
Sorting algorithms (quicksort, mergesort, heapsort)String algorithms and pattern matchingGraph algorithms: BFS, DFS and Dijkstra's shortest pathGreedy algorithms and minimum spanning treesDynamic programmingError-detecting and error-correcting codesData structures: arrays, linked lists, stacks, queuesHuman–computer interaction and usabilityHash tablesTesting and debugging