Algorithms are the building blocks of computer programs. They are as important to programming as recipes are to cooking.
Definition of algorithm
An algorithm is a well-defined procedure that takes input and produces output. The analogy to a recipe is good here, since a cook will take ingredients (the input), mix things together and cook it (the procedure), and produce a dish (the output).
The main difference here is that algorithms are mathematical or textual in nature. The algorithms we use in programming are any series of steps that produce an output we are interested in, for example, searching for an item in a list.
Kinds of algorithms
The basic kinds of algorithms include:
- searching, or looking for an item in a list
- sorting, or putting a list in alphabetic or numeric order
- shortest path, or finding the quickest way on a map from point A to point B
- compression, or reducing the size of a data file
That’s just a quick sample. There’s really unlimited different kinds of algorithms that are defined for any special purpose you can think of.
Discussion of algorithms
Once an algorithm is defined, we can name it and study its properties. The properties of an algorithm can be studied mathematically so that we know which ones will run faster given a larger amount of input.
An example would be sorting algorithms. A few common sorting algorithms are:
- insertion sort, like taking a deck of cards and inserting each card one by one into an already sorted order
- selection sort, like taking a deck of cards and going through the deck and finding the smallest card each time and putting it at the front of the deck
- merge sort, like taking a deck of cards and splitting it over and over again, sorting each partial deck, then merging them together in sorted order
- bubble sort, like taking a deck of cards, making multiple passes through it, and swapping adjacent pairs of cards that are out of order, until finally the smallest cards bubble to the top.
Order of algorithms
How fast an algorithm basically runs for a given size of input is called the order of the algorithm. We want algorithms with a smaller order, so their run time will not grow as fast when more input is added. Otherwise, your program will crawl to a halt.
Algorithms versus hardware
The average person who is not a programmer thinks we just need to throw more hardware at problems to solve them. However, the study of algorithms is actually more important, because in really bad cases, the wrong algorithm can make your program take millions of years to run. No amount of faster hardware can compensate for that.
When algorithms sometimes don’t matter to the programmer
A lot of modern programming languages will choose an algorithm and implement it for you, then just give you a function to call. For example, your programming language will probably offer searching and sorting functions. Then you can just call the function without worrying about which algorithm is used or how it is implemented. That’s the job of the language designers.