We've just looked at two different ways to write the same loop in Python. This brings up an important point: different algorithms can be used to achieve the same goals. This is because algorithms are, at their heart, steps to solve a problem, and there are many different ways to solve the same problem.
Often times, you'll see different algorithms used to solve the same problem, depending on the needs of the programmers and the program.
With all this variability, it's no wonder that new algorithms are constantly being developed.
New algorithms can be created from scratch or by combining and modifying algorithms that already exist. These existing algorithms could be ones you've written yourself or ones others have already created.
Existing Algorithm Types
Examples of Existing Algorithms:
- Determining the max. or min. value in a group of two or more numbers
- Solving math problems: calculating sums, averages, etc.
- Determining a robot's path through a maze (route-finding algorithm)
- Compressing data (see Big Idea 2)
- Sorting a list
Using existing algorithms can help reduce development and testing times because you're working with something that you can rely on. It also helps you simplify your debugging process because you know your problem can't come from within the algorithm.
If you use someone else's work, make sure to cite it properly, as mentioned in Big Ideas 1 and 5!

Frequently Asked Questions
How do I write an algorithm to find the maximum value in an array?
Simple approach (iterative, works with arrays): Pseudocode: 1. IF array is empty THEN report error/return null. 2. Set max ← array[0] 3. For position from 1 to length(array) − 1: - IF array[position] > max THEN max ← array[position] 4. Return max Why this works: you initialize max to a real element, then compare every other element once, updating only when you find a larger value. This is O(n) time and O(1) extra space—a standard max algorithm the AP CED lists as a common building block (EK AAP-2.M.2). Test with arrays of all negatives, duplicates, single-element, and empty to check side effects. You can refactor (e.g., start max = −infinity if exam language allows) or write equivalent Boolean/conditional forms—remember different-looking algorithms can be equivalent (EK AAP-2.L.*). For more examples and practice, check the Topic 3.9 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/developing-algorithms/study-guide/eFTUAVlAEU4XUX3MeQmF) and unit review (https://library.fiveable.me/ap-computer-science-principles/unit-3). For extra practice problems, see (https://library.fiveable.me/practice/ap-computer-science-principles).
What's the difference between two algorithms that do the same thing but are written differently?
If two algorithms “do the same thing” they might still differ in important ways you should check for AP Topic 3.9 (LO AAP-2.L): - Result vs side effects: They can produce the same final output but have different side effects (e.g., one modifies a global list, the other doesn’t). That’s EK AAP-2.L.2. - Correctness on all inputs: They might look equivalent but fail on edge cases—test with different inputs to prove equivalence (LO AAP-2.L). - Performance: One may use fewer steps, less memory, or be faster (time/space complexity). Different algorithms that solve the same problem are expected (EK AAP-2.M.2). - Readability & maintenance: Structure, use of procedures (modularization), and clarity affect debugging and reuse (EK AAP-2.M.3). - Logical form: Conditionals and Boolean expressions can be written differently but be equivalent (EK AAP-2.L.3–4); check by algebraic simplification or truth tables. When comparing, state whether they yield the same result and side effects, analyze complexity, and test with examples. For more practice and tips on algorithm equivalence and developing algorithms, see the Topic 3.9 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/developing-algorithms/study-guide/eFTUAVlAEU4XUX3MeQmF) and try problems at (https://library.fiveable.me/practice/ap-computer-science-principles).
Why does my `if` statement give a different result than my Boolean expression even though they look the same?
They can look the same but behave differently for a few common reasons—mostly because conditionals introduce control flow and side effects while a Boolean expression is just a value: - Assignment vs comparison: using = or := instead of == (or = in pseudocode) will change state and make the if behave differently. - Side effects or mutation: code inside the if (or called before it) might change variables used in the expression, so the result differs from the “pure” Boolean. EK AAP-2.L.2 covers that idea (algorithms that appear similar can yield different side effects). - Short-circuit evaluation: (A AND B) won’t evaluate B if A is false—any function or expression in B won’t run. That changes side effects. - Type/coercion or truthy/falsy rules: languages treat values differently (0, "", null) which can make an expression’s truthiness differ from what you expect. - Parentheses/precedence: missing parens can change which parts are grouped. On the AP exam you may need to show equivalence between a conditional and a Boolean expression (EK AAP-2.L.3/4). If you want, paste your code and I’ll point out which of the above is happening. For more on developing algorithms and equivalence, check the Topic 3.9 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/developing-algorithms/study-guide/eFTUAVlAEU4XUX3MeQmF) and practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).
Can someone explain how to combine existing algorithms to make a new one?
Think of combining algorithms like building with Lego blocks: each existing algorithm (find max, sum numbers, check divisibility, maze pathfinder) is a tested block you can connect to make a bigger solution. How to do it: - Identify subproblems. Break the overall task into parts that match known algorithms (EK AAP-2.M.2–2.M.3). - Pick or write small procedures for each part (procedural abstraction). Give clear inputs/outputs and avoid hidden side effects so blocks compose correctly (EK AAP-2.L.2, EK AAP-2.M.1). - Compose them in pseudocode: call subroutines in order, pass needed data, handle edge cases, and document assumptions. - Modify blocks if necessary (e.g., change a max-of-two to max-of-list) and re-test. Comparing alternative compositions checks equivalence and side effects (LO AAP-2.L). - Test thoroughly with boundary cases and show correctness—this helps on both the exam and the Create task. For practice and examples of composing/modifying algorithms, see the Topic 3.9 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/developing-algorithms/study-guide/eFTUAVlAEU4XUX3MeQmF) and hundreds of practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).
I'm confused about when two algorithms are actually equivalent - how do I tell?
Two algorithms are equivalent only if, for every possible input and starting state, they produce the same result and the same side effects (EK AAP-2.L.1, AAP-2.L.2). To tell: - Define what “result” and “side effects” mean for the problem (return value, changed files, global variables, outputs). - Compare outputs for every input (or every case class). If domain is small, exhaustively test; if large, use representative/edge-case tests and reasoning. - Check control flow and termination: both must halt (or both loop) for the same inputs. - For conditionals/Boolean logic, try algebraic equivalence (EK AAP-2.L.3/4): transform conditions into equivalent Boolean expressions. - If unsure, produce a counterexample input showing different result or side effect—one counterexample proves non-equivalence. - Use modular reasoning: show equivalent subroutines are used (EK AAP-2.M). Practice this with examples in the Developing Algorithms study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/developing-algorithms/study-guide/eFTUAVlAEU4XUX3MeQmF) and run lots of practice problems (https://library.fiveable.me/practice/ap-computer-science-principles). On the AP exam you’ll be asked to justify equivalence or give counterexamples, so focus on clear tests and logical explanations.
What's the syntax for writing a Boolean expression that's equivalent to multiple `if` statements?
If you have several if/else checks that decide one action, you can combine their tests into a single Boolean expression using AND (∧ / &&), OR (∨ / ||), and NOT (¬ / !), with parentheses to control order. This is exactly EK AAP-2.L.3: some conditionals can be written as equivalent Boolean expressions. Examples (pseudocode): - Multiple ifs: if (age >= 16) and if (age < 18) then eligible - Combined: if ((age >= 16) AND (age < 18)) then eligible Using OR: if ((x == 1) OR (y == 2) OR (z == 3)) then doAction Use NOT to invert: if (NOT (isEmpty) AND (count > 0)) then proceed Always use parentheses to make precedence explicit. For AP exam-style pseudocode, follow the Exam Reference Sheet operators and practice converting equivalent conditionals (see Topic 3.9 study guide on Fiveable: https://library.fiveable.me/ap-computer-science-principles/unit-3/developing-algorithms/study-guide/eFTUAVlAEU4XUX3MeQmF). For more practice, check the unit practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).
How do I modify an existing sorting algorithm to work with my specific problem?
Start by asking what’s special about your problem: data type (numbers, strings, objects), sort key (age, name), stable order needs, memory limits, and size. Then modify an existing sort (like insertion, merge, quick) by these steps: - Change the comparison to use your key or a comparator (e.g., compare names case-insensitively or compare object.field). - Decide stability: use merge or insertion for stable results; quicksort can be unstable unless tweaked. That matters for side effects (EK AAP-2.L.2). - Adjust bounds: if you only need top-k, use partial sort or selection algorithms to cut time/space. - Add early-exit checks (already sorted) to improve best-case time. - If memory is tight, prefer in-place sorts (EK AAP-2.M.1). - Modularize: wrap key-extraction and comparison so you can reuse and test pieces (EK AAP-2.M.3). Finally, prove correctness and test with edge cases (duplicates, empty list, one item)—AP exam wants you to compare algorithms, note side effects, and show testing/refactoring. See the Topic 3.9 study guide for examples and pseudocode (https://library.fiveable.me/ap-computer-science-principles/unit-3/developing-algorithms/study-guide/eFTUAVlAEU4XUX3MeQmF). For more practice, check unit resources (https://library.fiveable.me/unit-3) and 1000+ practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).
Why am I getting different outputs from two algorithms that should do the same thing?
If two algorithms “should” do the same thing but give different outputs, it usually comes down to differences in side effects, control flow, or assumptions. Common causes: - different initial values or uninitialized variables; - off-by-one errors in loops (loop bounds differ); - different order of operations or updates (mutation of shared data); - one uses integer vs. floating-point math or different rounding; - hidden side effects (modifying an input list vs returning a new list); - nondeterminism (randomness or reliance on unordered sets); or - different input preconditions (one expects sorted input, the other doesn’t). How to fix it: pick representative test cases (including edge cases), trace both algorithms step-by-step or use invariants (what must be true at each loop iteration), and compare control flow. The AP CED’s LO AAP-2.L expects you to compare algorithms’ side effects/results and spot these differences—so practice tracing and testing. For more examples and tips see the Topic 3.9 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/developing-algorithms/study-guide/eFTUAVlAEU4XUX3MeQmF), the Unit 3 overview (https://library.fiveable.me/ap-computer-science-principles/unit-3), and lots of practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).
When should I use an existing algorithm vs writing my own from scratch?
Use an existing algorithm when it already matches the problem, is correct, and saves time/testing. The CED explicitly says algorithms can be combined or modified and that using correct, existing algorithms reduces development time and testing (EK AAP-2.M.1, AAP-2.M.3). Pick an existing one if its side effects/results match what you need (LO AAP-2.L)—but check for different side effects even if it looks similar (EK AAP-2.L.2). Write your own when the problem has special constraints, needs a different side effect, or when no known algorithm fits well (so you may combine or modify known algorithms—EK AAP-2.M.1). Always test for correctness and equivalent boolean/conditional forms (EK AAP-2.L.3–4). For AP tasks, cite reused algorithms and explain any modifications in your Create submission (plagiarism rules in the CED). For study and examples, see the Topic 3.9 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/developing-algorithms/study-guide/eFTUAVlAEU4XUX3MeQmF) and more unit review (https://library.fiveable.me/ap-computer-science-principles/unit-3) or practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).
I don't understand how to trace through two different algorithms to see if they give the same result - can someone help?
Think of tracing like running both algorithms by hand for the same inputs and watching every variable and side effect. Do this: 1. Pick test inputs—include typical, boundary, and special cases (empty lists, equal values, max/min values). 2. For each input, step through both algorithms line-by-line, writing the value of each variable after every step and noting any side effects (e.g., modifying a list, printing, or writing to storage). 3. Compare final outputs and side effects for every test. If any differ for any input, the algorithms aren’t equivalent (EK AAP-2.L.2, AAP-2.L). 4. If tests match, try to prove equivalence by reasoning about control flow and Boolean expressions (transform conditionals into equivalent Boolean forms—EK AAP-2.L.3/4). 5. Document edge cases that matter (off-by-one loops, order of updates, short-circuiting). Practice this method on AP-style problems—it’s exactly what’s tested in Topic 3.9. See the Topic 3 developing algorithms study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/developing-algorithms/study-guide/eFTUAVlAEU4XUX3MeQmF) and try more problems at Fiveable (https://library.fiveable.me/practice/ap-computer-science-principles).
What are some common existing algorithms I should know for finding averages and checking divisibility?
Common algorithms to know for averages and divisibility (AP CSP Topic 3.9): - Averages: - Simple mean: sum all values then divide by count (useful as a single-pass algorithm). This is in EK AAP-2.M.1/2—you can implement as two-step (compute sum, compute average) or a running average (update average each new value to save memory). - Weighted average: multiply each value by its weight, sum those products, divide by total weight. - Moving/window average: keep a fixed-size buffer and update sum as items enter/leave (good for streaming data). - Note: different implementations yield the same result if done correctly, but can differ in side effects (precision, memory, number of passes)—tie to LO AAP-2.L. - Divisibility: - Remainder test: compute n % d == 0 (or integer division with remainder)—canonical algorithm for “evenly divisible” (EK AAP-2.M.2). - Common digit tests (fast checks without division): divisible by 2 (last digit even), 3/9 (sum of digits), 5 (last digit 0 or 5), 11 (alternating sum). Use these as simple building-block algorithms. - Euclidean algorithm finds gcd—useful to test divisibility relationships and simplify fractions. These are exactly the kinds of algorithms you’ll combine/compare on the exam (see LO AAP-2.L and AAP-2.M). For a focused review, check the Topic 3.9 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/developing-algorithms/study-guide/eFTUAVlAEU4XUX3MeQmF) and practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).
How do I write an algorithm that combines finding the minimum value with computing an average?
You can combine finding the minimum and computing an average in one pass over the list—that’s efficient and aligns with AAP-2.M (combine existing algorithms). Pseudocode: Set sum ← 0 Set count ← 0 Set minVal ← +infinity FOR EACH item IN list sum ← sum + item count ← count + 1 IF item < minVal THEN minVal ← item END FOR average ← sum / count Output minVal and average Why this works: you reuse the sum/average algorithm and the min algorithm together (EK AAP-2.M.2). It has O(n) time and no extra side effects beyond sum, count, and minVal (check for empty list to avoid divide-by-zero). For AP exam style answers, explain correctness and complexity briefly and test with small lists (e.g., [3,7,1,5]). For more examples and practice problems, see the Topic 3.9 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/developing-algorithms/study-guide/eFTUAVlAEU4XUX3MeQmF) and lots of practice questions (https://library.fiveable.me/practice/ap-computer-science-principles).
What's the difference between side effects and results when comparing algorithms?
A result is the explicit output an algorithm is supposed to produce—the value(s) or final state you check to see if the algorithm solved the problem (e.g., the max value, the sorted list, or a count). A side effect is any additional change the algorithm makes to the program state or environment that isn’t the main output—things like modifying global variables, updating a database, printing intermediate values, or moving a robot. Two algorithms can have the same result but different side effects (EK AAP-2.L.2). For AP questions, focus on both: confirm the algorithms produce equivalent results and check for differing side effects that could break correctness or reuse (EK AAP-2.L.1, AAP-2.M). Review examples in the Topic 3.9 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/developing-algorithms/study-guide/eFTUAVlAEU4XUX3MeQmF) and practice comparing pairs of algorithms on Fiveable’s practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).
Can Boolean expressions always be rewritten as conditional statements and vice versa?
Short answer: no—sometimes yes, but not always. The CED explicitly says “some” conditional statements and Boolean expressions can be made equivalent (EK AAP-2.L.3–4). A Boolean expression (like x > 5 AND y = 0) just evaluates true/false. A conditional statement (if ... then ...) can use that expression to choose actions. You can often rewrite a simple if that returns true/false as just a Boolean expression, and vice versa for a pure test that has no side effects. Why not always? Conditional statements can contain side effects (assignments, I/O, function calls) and control flow (else, nested blocks). Those change program state and can’t be captured by a plain Boolean expression. Also language features (short-circuit evaluation, order of evaluation, exceptions) can make two formulations behave differently even if they look logically equivalent. For AP CSP, focus on recognizing equivalent logic when there are no side effects (EK AAP-2.L.3–4). For practice and examples, check the Topic 3.9 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/developing-algorithms/study-guide/eFTUAVlAEU4XUX3MeQmF) and Unit 3 resources (https://library.fiveable.me/ap-computer-science-principles/unit-3). For extra practice, use the AP practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).
How do I debug when my modified algorithm doesn't work the same as the original one?
First, check whether your modified algorithm is supposed to be equivalent or just “close.” If it must produce the same result/side effects (LO AAP-2.L / EK AAP-2.L.2), do this: 1. Re-state the intended effect and side effects (what changes, what’s returned). 2. Make small, targeted tests: include typical, boundary, and edge cases (empty, max/min, equal values). 3. Dry-run and trace both algorithms step-by-step for a failing test—track variables, loop counters, and any side effects (file/array updates). 4. Compare control flow and Boolean logic: an equivalent conditional can be written differently but behave differently (EK AAP-2.L.3/4). Look for off-by-one, wrong comparison operators, or misplaced updates. 5. Isolate components (modularize). Test subroutines individually (EK AAP-2.M.3). 6. Use assertions or print/log statements to check invariants inside loops. 7. If still stuck, revert to the original and apply one change at a time, retesting after each change. For more examples and practice on developing and comparing algorithms, see the Topic 3.9 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/developing-algorithms/study-guide/eFTUAVlAEU4XUX3MeQmF). For broader review and 1000+ practice problems, check Unit 3 (https://library.fiveable.me/ap-computer-science-principles/unit-3) and practice (https://library.fiveable.me/practice/ap-computer-science-principles).