What Is an Algorithm
An algorithm is a precise, step-by-step procedure for solving a problem or completing a task, written clearly enough that it can be followed without any guessing — and it is the idea at the heart of every program, because a program is simply an algorithm written in a language a computer can run. You already use algorithms outside of computers: a recipe is one, directions to a friend's house are one, and the method you learned for long division is one. What makes a procedure a real algorithm is a short list of requirements: it takes some input (the data it starts with), it produces an output (the answer or result), every step is definite (clear enough that no judgment call is needed), and it always finishes after a limited number of steps instead of running forever. The computational-thinking topic described how to break a problem into parts and write the steps as pseudocode; an algorithm is the finished result of that work, the exact sequence of steps you settled on. It is worth separating the algorithm from the program that carries it out: the algorithm is the idea, and the program is one particular way of writing that idea down in Python, JavaScript, or C, so the very same algorithm can be written in many languages and still do the same job. Consider finding the largest number in a list. Start by remembering the first number as the biggest so far, then look at each remaining number in turn, and whenever one is larger than the remembered number, replace the remembered number with it; when the list runs out, the remembered number is the answer. That procedure has an input (the list), an output (the largest number), steps that need no guesswork, and a guaranteed end, because the list is finite. Notice that it uses the building blocks from the programming-basics topic: a variable to remember the biggest value, a loop to visit each item, and a condition to compare. A second famous example is binary search, a way to find a name in a sorted list by checking the middle item and then throwing away the half that cannot contain the target, repeating until it is found, which is how you naturally look up a word in a printed dictionary rather than reading every page from the front. The same problem can usually be solved by more than one algorithm, and the choice matters a great deal: scanning a list from the start works for any list, while binary search needs the list to be sorted first but is dramatically faster, because each check discards half of what remains, so a million items need only about twenty checks instead of up to a million. Computer scientists compare algorithms mainly by how much time and memory they need as the input grows, a measurement the big-O topic covers in more depth, and also by whether the algorithm gives a correct answer in every case, since a fast algorithm that is sometimes wrong is not useful. Correctness is checked by testing it on ordinary inputs and also on awkward edge cases such as an empty list or a list with repeated values, which is exactly the habit the debugging-and-testing topic describes. Algorithms are not always exact recipes with a single right answer either: a search engine uses ranking algorithms to decide which pages to show first, a map app uses a shortest-path algorithm to choose a route, and a machine-learning system uses a learning algorithm that adjusts itself from examples, so the word covers everything from a three-line loop to the large systems that shape what people see online. Because an algorithm decides things, its design can carry real consequences, which is why the computing-ethics topic asks whether the rules and data behind an algorithm treat people fairly. Tying it together, an algorithm is not a mysterious or purely technical idea: it is a clear, finite, step-by-step method that turns an input into an output, written first as an idea or pseudocode and then as a program, and judged by whether it is correct and how efficiently it uses time and memory.