Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

Algorithms are step-by-step procedures for solving problems, from sorting a list to finding the shortest path on a map. Complexity describes how much time or memory an algorithm needs as the input size grows. This matters because two correct programs can behave very differently when given large data sets.

Computer scientists use complexity to predict performance before running code.

Understanding Algorithms and Complexity

Sorting is more than putting values in order. Different methods make different tradeoffs. Insertion sort builds a sorted section by placing each new item into its proper position.

It can work well when a list is already nearly sorted or very small. Merge sort splits a list into smaller parts, sorts those parts, then combines them. This gives reliable growth for large lists, but it usually needs extra memory while merging.

Some sorts are stable, meaning equal items keep their original order. Stability matters when records are sorted by more than one field, such as students first by name, then by score.

Searching depends strongly on how data is stored. A sorted array supports binary search because the middle value tells the program which half can be ignored. The same idea does not work directly with an unsorted pile of information.

In a linked list, reaching the middle may itself require moving through many links, so binary search loses its advantage. Databases often use tree-based indexes for a similar reason.

An index takes space and time to build, but it makes repeated searches much faster. This is why a school library catalog can find a book record quickly without checking every book.

Big O is useful for comparing growth, not for predicting an exact running time. It ignores fixed costs, such as setting up a loop, because those costs become less important as the input becomes huge. It also ignores the speed of a particular computer.

A method with linear growth can be slower than another method on tiny inputs if it has a large fixed cost. Input shape matters too. A sorting method may be fast for random data yet slow when values arrive in an unlucky order.

Good analysis considers worst case, average case, and sometimes best case. Memory use needs the same care, since a fast method can be a poor choice when a phone or small device has limited storage.

Students often meet complexity when writing loops, choosing data structures, or processing files. Two separate loops are usually proportional to the number of items, while a loop inside another loop can compare many pairs. The important habit is to count how the work changes when the input doubles.

Test programs with small and larger data sets, then measure time using the same task each time. Measurements can be noisy because of other programs, caching, or file access, so repeat them. Learn to state assumptions clearly.

For example, binary search requires sorted data, and sorting first may not help if only one search is needed. The best algorithm depends on the job, the data, and the limits of the device.

Key Facts

  • Linear search checks items one by one, so its time complexity is O(n).
  • Binary search on a sorted list has time complexity O(log n).
  • A simple nested loop over n items often has time complexity O(n^2).
  • Big O notation describes an upper bound on growth rate as input size becomes large.
  • If an algorithm does c steps per item, then T(n) = cn + k is simplified to O(n).
  • Common growth order from fastest to slowest is O(1), O(log n), O(n), O(n log n), O(n^2), O(2^n).

Vocabulary

Algorithm
An algorithm is a finite set of clear steps used to solve a problem or complete a task.
Time complexity
Time complexity describes how the number of operations grows as the input size increases.
Space complexity
Space complexity describes how much memory an algorithm uses as the input size increases.
Big O notation
Big O notation expresses the approximate upper growth rate of an algorithm while ignoring constants and smaller terms.
Input size
Input size is the amount of data an algorithm must process, often represented by n.

Common Mistakes to Avoid

  • Counting seconds instead of operations is wrong because running time depends on hardware, language, and implementation details.
  • Keeping constant factors in Big O is wrong because O(3n + 10) simplifies to O(n) for large-input growth analysis.
  • Assuming every loop makes an algorithm O(n) is wrong because nested loops, halving loops, and early exits can change the growth rate.
  • Ignoring the input requirements is wrong because binary search is O(log n) only when the data is already sorted.

Practice Questions

  1. 1 An algorithm performs 5n + 20 operations. What is its Big O time complexity, and about how many operations does it perform when n = 100?
  2. 2 A nested-loop algorithm compares every pair in a list, taking about n^2 operations. How many operations occur for n = 50, and how many for n = 200?
  3. 3 You need to search for a name in a phone book that is already alphabetized. Explain why binary search is more efficient than linear search for a very large phone book.