From the course: Programming Foundations: Algorithms
What are algorithms? - Python Tutorial
From the course: Programming Foundations: Algorithms
What are algorithms?
- Let's get started by understanding what an algorithm is by working through a hypothetical example. Suppose you had a collection of shapes and you needed to group all of the same shapes together in individual sets. There's a variety of ways you could do this, but probably the simplest way would be to write some kind of loop function to iterate over each of the shapes, determine what each kind of shape is, and then add that shape to the correct group. At the end of this process, you would have separate groups of each shape type. This complete set of steps taken together that solve a specific problem forms an algorithm. Algorithms have several characteristics that can be used to describe them. For example, any given algorithm has an associated complexity, and usually more than one. There's space complexity, which describes how much memory and storage space the algorithm needs to do its work. There's also time complexity, which describes how efficient the algorithm is relative to the size of the input it is given to work on. We'll learn more about time complexity later in the chapter. Algorithms typically also have a defined set of inputs and output. In other words, any given algorithm has a set of input values it can work on in order to produce a result. Sorting algorithms, for example, take a set of data values and attempt to put them into a specific order. It's also possible to talk about algorithm classification using a variety of criteria. Some algorithms work on their data sets in sequential fashion, which means that they are serial in nature. A parallel algorithm can break up a data set into smaller pieces and then work on them simultaneously. An algorithm can be exact, in which case it produces a known predictable value, or it can be approximate, in which case it tries to find an answer that might or might not be exact. For example, a facial recognition algorithm might not give the same answer every time. Algorithms can be deterministic, in which case it executes each step with an exact decision, or it can be non-deterministic, in which it attempts to produce a solution using successive guesses which become more accurate over time. Of course, there's much more to the field of algorithms than I can capture in just a few bullet points. We won't have time to get into all of these different intricacies in this course since it's just an introduction to the subject, but this should give you a basic sense of what algorithms are and how they work. Next, we'll learn a little more about some of the common algorithms you'll encounter when writing your own programs.