Welcome to Developing Algorithms!

Ever wonder how a computer "thinks"? It doesn't actually have a brain; it follows a set of precise instructions called an algorithm. In this chapter, we are going to look at how we build these instructions using three basic building blocks. Don't worry if this seems like a lot to take in—once you see how these blocks fit together, you'll realize you've been "programming" algorithms in your head your entire life!

What is an Algorithm?

An algorithm is a finite set of instructions used to accomplish a specific task or solve a problem. Whether you are tying your shoes, baking a cake, or searching for a contact in your phone, you are following an algorithm.

In AP Computer Science Principles, we focus on how to create, combine, and modify these algorithms to make them efficient and effective.

The Three Building Blocks

Every algorithm in the world, no matter how complex (even the ones running social media feeds!), is built using just three basic patterns. Think of these as the "LEGO bricks" of computer science:

1. Sequencing

Sequencing is the most basic block. It simply means putting steps in a specific order. The computer executes the statements one after another, from top to bottom.

Analogy: Putting on your socks before putting on your shoes. If you swap the order, the result is very different!

2. Selection

Selection (also known as a "conditional") allows the algorithm to make a decision based on a condition. It uses "Boolean" logic, which means the answer to the question must be either true or false.

Example: IF it is raining, THEN bring an umbrella; ELSE wear sunglasses.

Note: For a deeper look at this, check out our chapters on Conditionals and Nested Conditionals.

3. Iteration

Iteration is just a fancy word for "looping" or repeating a set of steps. We use this when we want to do the same thing multiple times without writing the code over and over.

Example: REPEAT 3 TIMES: Stir the batter.

Note: You can learn more about this in the Iteration chapter.

Quick Review: Algorithms are made of Sequencing (order), Selection (deciding), and Iteration (repeating).

Ways to Represent Algorithms

You don't always need a computer to write an algorithm! You can express them in several ways:

1. Natural Language: Just plain English (or your preferred language). Great for brainstorming but can be too vague for computers.
2. Pseudocode: A mix of natural language and "code-like" structures. It’s the "middle ground" that helps humans plan for computers. The AP Exam uses a specific version of pseudocode found on your Exam Reference Sheet.
3. Flowcharts: A visual diagram representing the logic. On the AP Exam, remember these shapes:
- Oval: The start or end of the algorithm.
- Parallelogram: An input or output step (like reading a sensor or displaying text).
- Rectangle: A process or calculation (like \( x \leftarrow x + 1 \)).
- Diamond: A decision/selection point (Yes/No branches).

Developing and Modifying Algorithms

You don't always have to reinvent the wheel! Many great programs are created by combining or modifying existing algorithms.

Why modify an existing algorithm?

- It saves time.
- It reduces the chance of making new errors (since the old algorithm was already tested).
- It makes complex problems easier to solve by breaking them into smaller, familiar pieces.

Common Mistake: Students often think they need to start from scratch every time. In reality, "copying" your own logic from a previous task and tweaking it is a sign of a smart programmer!

Comparing Algorithms

Sometimes, two different algorithms can produce the same result even if they look different. We call these equivalent algorithms.

When comparing them, we look at:
- The Result: Do they both give the correct answer for all possible inputs?
- The Side Effects: Does one algorithm change a variable that the other doesn't? A "side effect" is any change the algorithm makes to the program's state beyond its main return value.

Example: Imagine two algorithms to find the sum of \( 1 + 2 + 3 \).
- Algorithm A: \( 1 + 2 + 3 \)
- Algorithm B: \( 3 \times 2 \)
Both result in \( 6 \), so they are equivalent in their result!

Quick Tips for Success

- Hand Tracing: If you're stuck on what an algorithm does, grab a piece of paper. Track the values of the variables step-by-step. This is the #1 way to catch logic errors!
- Stay within the limits: On the AP exam, you only need to worry about what is on the Reference Sheet. Don't worry about complex "Big-O" math or formal analysis—that's for later college courses!
- Think logically: If a question asks you to "complete the algorithm," look at the patterns of Sequencing, Selection, and Iteration to see what's missing.

Key Takeaway: Developing algorithms is about using the three building blocks (Sequence, Selection, Iteration) to solve problems. You can represent these ideas in flowcharts or pseudocode, and you can always build on existing ideas to create something new.