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 wikiHow teaches you how to piece together an algorithm that gets you started on your application.
How to Write an Algorithm
- Know the outcome of your code.
- Find the starting and ending point.
- List the steps from start to finish.
- Find how you’ll accomplish each step.
- Look over your work and evaluate the process.
Steps
-
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: [1] X Research source
- 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?
Advertisement -
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: [2] X Research source
- 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. [3] X Research source 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.
- 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.
- 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:
-
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: [4] X Research source
- 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?
Expert Q&A
-
QuestionHow do I plan or map out an algorithm process?Tyrone Showers is a Technologist and the Co-owner of Taliferro Group, an IT consulting company based in Seattle, Washington. With over 35 years of professional experience, he specializes in API Design, e-Commerce, Operational Efficiency, and website development. He has a B.S. in Computer Science from DeVry Institute of Technology.Lay the groundwork first, learn some of the common algorithms, and understand what they do and what they're attempting to do. Bayes is one of my favorite algorithms. I build on this in my code to develop software agents that pick up new skills depending on previous behavior.
-
QuestionHow do I write an algorithm that 7 is greater than 5?nicholasz2510 Gaming, Travel, and MusicCommunity AnswerThe syntax can vary over different languages, but to write the conditional 7 is greater than 5 would most likely by simply be this: 7 > 5.
-
QuestionHow do I make an algorithm of the sum of two numbers?Adam BlalockCommunity AnswerTo add two numbers in a programming language, you just use a "+" between them. In Python (a programming language), it would look like: x = 10, y = 13; print x + y.
Tips
- Check out existing algorithms for ideas on writing your own.Thanks
- Use fast calculating iterations.Thanks
- Focus on efficiency when coding.Thanks
Expert Interview
Thanks for reading our article! If you’d like to learn more about writing algorithms, check out our in-depth interview with Tyrone Showers .
References
- ↑ https://www.freecodecamp.org/news/the-programming-language-pipeline-91d3f449c919/
- ↑ https://www.freecodecamp.org/news/the-programming-language-pipeline-91d3f449c919/
- ↑ https://www.geeksforgeeks.org/introduction-to-algorithms/
- ↑ https://www.khanacademy.org/computing/ap-computer-science-principles/algorithms-101/evaluating-algorithms/a/verifying-an-algorithm
About This Article
1. Determine the problem or task to accomplish.
2. Decide the starting point.
3. Figure out the endpoint.
4. List the steps that occur between the start and finish.
5. Break down the steps as necessary.
6. Review the algorithm and change where necessary.