An algorithm is a set of steps designed to solve a problem or accomplish a task. Algorithms are usually written in pseudocode, or a combination of your speaking language and one or more programming languages, in advance of writing a program. This minHour teaches you how to piece together an algorithm that gets you started on your application.
Determine the outcome of your code.
What is the specific problem you want to solve or the task you want it to accomplish? Once you have a solid idea of what you’re aiming to accomplish, you can determine the steps it will take to get there.
Decide on a starting point.
Finding your starting and ending point are crucial to listing the steps of the process. To determine a starting point, determine the answers to these questions:
- What data/inputs are available?
- Where is that data located?
- What formulas are applicable to the issue at hand?
- What are the rules to working with the available data?
- How do the data values relate to each other?
Find the ending point of the algorithm.
As with the starting point, you can find the end point of your algorithm by focusing on these questions:
- What facts will we learn from the process?
- What changes from the start to the end?
- What will be added or no longer exist?
List the steps from start to finish.
Start with broad steps. To use a real-world example, let’s say your goal is to have lasagna for dinner. You’ve determined that the starting point is to find a recipe, and that the end result is that you’ll have a lasagna fully cooked and ready to eat by 7 PM. Your steps may look something like this:
- Search for a recipe online.
- Look for the ingredients you already have in the kitchen.
- Make a list of ingredients you’ll need from the store.
- Buy the missing ingredients.
- Return home.
- Prepare the lasagna.
- Remove the lasagna from the oven.
Determine how you will accomplish each step.
Now that you have a step-by-step outline, it’s time to think about how you might code each step. Which language will you use? What resources are available? What’s the most efficient way to accomplish each step in that language? Incorporate some of that code into your algorithm. Expand each step until you’ve detailed the entire process.
- For example, the first step in our lasagna algorithm is Search for a recipe online. But what is involved in this search? Be specific. For example:Turn on your computer.Check to make sure you’re connected to the internet. Connect to the internet if you aren’t already.Open a web browser.Enter your search terms.Click a recipe link.Determine whether the recipe meets your needs.Filter out recipes that aren’t vegetarian.Make sure the recipe makes at least 5 servings.Repeat some of these steps until you find the right recipe.
- Turn on your computer.Check to make sure you’re connected to the internet. Connect to the internet if you aren’t already.
- Check to make sure you’re connected to the internet. Connect to the internet if you aren’t already.
- Open a web browser.
- Enter your search terms.
- Click a recipe link.
- Determine whether the recipe meets your needs.Filter out recipes that aren’t vegetarian.Make sure the recipe makes at least 5 servings.
- Filter out recipes that aren’t vegetarian.
- Make sure the recipe makes at least 5 servings.
- Repeat some of these steps until you find the right recipe.
- Consider the resources at your disposal, such as the capabilities of the system you’re developing a program for. In the case of lasagna, we assume the person making the lasagna knows how to search the internet, operate an oven, etc.
Review the algorithm.
Now that you’ve written your algorithm, it’s time to evaluate the process. Your algorithm is designed to accomplish something specific, and you’ll need it to start writing your program. Ask yourself the following questions, and address each as necessary:
- Does the algorithm solve the problem/accomplish the task?
- Does it have clearly defined inputs and outputs?
- Should the end goal be redefined to be more general? More specific?
- Can any of the steps be simplified?
- Is the algorithm guaranteed to end with the correct result?
- Check out existing algorithms for ideas on writing your own.
- Use fast calculating iterations.
- Focus on efficiency when coding.