Skip to main content
Logo image

Chapter 12 Algorithms

An algorithm is a step-by-step procedure that can be followed to solve a problem or perform a task. (The word comes from the Latinization of the name of the eighth-century Persian mathematician Muhammad ibn Musa al-Khwarizmi who also gave us the term algebra.)
Since every computer program is a step-by-step procedure, in some sense every computer program is an algorithm. However, when programmers and computer scientists talk about algorithms, we’re usually talking about something simpler and more general than a whole program. We can talk about algorithms and how they work indendently of any particular program that might implement a given algorithm.
As programmers we need to know about some practical algorithms for solving problems that come up in many programs so we can apply those ideas in our programs without having to invent them from first principles. Computer scientists also study algorithms to understand what it is even theoretically possible to do with computers and what limits there are on how efficient algorithms can be.
In this chapter we will look at two categories of the most basic and useful algorithms that are used in lots of programs: algorithms for searching through a collection such as an array, a String or an ArrayList and algorithms for sorting the values in an array or ArrayList, such as alphabetizing a list of names. We will also look at recursion, a technique for implementing certain algorithms, including searching and sorting algorithms, that sometimes makes it easier to express the algorithm.