A good way to talk about algorithms is as a series of simple steps that can be repeated. It's similar to the role of a function or routine. Here, everyday examples can be helpful, for example something like autocorrect. Autocorrect is a series of steps repeated over and over.
- Select the last word typed
- Check how similar it is to the other words that the computer knows
- Check how often the user has used each of these words
- Suggest the most similar and most used word
- If the user types a space, make the correction
It will repeat these steps every time a character is typed. When writing code, it's clearly easier to write:
function check_last_word(text) {
word = text.last_word
similar_words = get_similar_words(dictionary, word)
corrected_word = get_most_used_word(word_usage_data, similar_words)
text = text - text.last_word + corrected_word
}
when text_is_changed? {
check_last_word(text)
}
As compared to:
word = text.last_word
similar_words = get_similar_words(dictionary, word)
corrected_word = get_most_used_word(word_usage_data, similar_words)
text = text - text.last_word + corrected_word
sleep until text_is_changed?
word = text.last_word
similar_words = get_similar_words(dictionary, word)
corrected_word = get_most_used_word(word_usage_data, similar_words)
text = text - text.last_word + corrected_word
sleep until text_is_changed?
word = text.last_word
similar_words = get_similar_words(dictionary, word)
corrected_word = get_most_used_word(word_usage_data, similar_words)
text = text - text.last_word + corrected_word
sleep until text_is_changed?
word = text.last_word
similar_words = get_similar_words(dictionary, word)
corrected_word = get_most_used_word(word_usage_data, similar_words)
text = text - text.last_word + corrected_word
sleep until text_is_changed?
word = text.last_word
similar_words = get_similar_words(dictionary, word)
corrected_word = get_most_used_word(word_usage_data, similar_words)
text = text - text.last_word + corrected_word
sleep until text_is_changed?
And so on. This is not actually an example of an algorithm, but it acts as a good example to show why having a series of repeatable steps is better than simply going through each step each time.