A computer algorithm is, simply, a set of rules a program uses to solve a problem, in preferably as few steps as possible. Algorithms can be used for small tasks, or they can be at the centerpiece of giant companies; the algorithm Google uses to get search results is massive, and is constantly being altered (and a well-kept secret). Sophisticated algorithms use events and conditionals to achieve the best result.
The basic idea behind algorithms--and really, programs in general--is to decide what you want to accomplish and then keep breaking it into steps. For example, say you want to play tennis. You might have to do the following:
- Call some friends and invite them to play.
- Gather your stuff.
- Drive to the tennis court.
Of course, it doesn't always happen like that. So you might want to refine that both be more specific and cover every possible result.
- Call a friend and invite them to play. If the friend says no, repeat this step with another friend. If the friend says yes, go to Step #2.
- Decide a time and location to play.
- Locate your tennis racket.
- Put your tennis racket in your car.
- Ten minutes before [time] occurs, drive to [location].
As you can see, you could keep breaking this down into further steps and further alternative scenarios.
Ingredients of Algorithms
The more complicated algorithm above actually contains a number of programming principles. Take a look at Step #1. That's a basic programming loop: keep doing a certain task (calling friends) until a condition is met (one says yes).
You could also look at it as a "case" or an "if/then" statement. In the case of "yes," go to Step #2. In the case of "no," go to Step #1. Or, if "yes," go to Step #2. If any other answer, repeat Step #1. You have to personally decide the best method to use for each particular algorithm.
Step #2 creates two variables: a time, and a location. You can plug them into your program later, in Step #5, when your actions are dependent on the values of the two variables.
Finally, Step #5 contains an event: when [time] minus ten minutes occurs, get in your car.
Error Checking and Comprehensiveness
One of the major difficulties behind creating algorithms is the necessity to cover every scenario. This is most obviously illustrated in creating programs that depend on user input. For example, in a video game, what if the player decides to kill a character that's vital to the plot? Do you make the character indestructible? Do you create an alternative victory condition? In an online form, what if you ask users to input a number between one and 10 and they input "bob"? In order to create the sturdiest algorithm possible, you must think out all possible outcomes and build in checks for each of them.
Tricks for Memorizing ACLS Algorithms
Memorizing things can be quite difficult to do, but if you know a few good tricks, you can easily commit to memory...
Types of Search Algorithms
Search algorithms are an important part of many programs. Some searches involve looking for something in a database --- like looking up...
How to Understand Algorithms
In simplest terms, learning how to understand algorithms requires little more than the ability to visualize lists of fixed directives that offer...