Definition of Computer Algorithms

This is a writing sample from Scripted writer Peter Naudus

  1. Definition An algorithm is a series of instructions, similar to a recipe. Merriam-Webster defines an algorithm as: "A step-by-step procedure for solving a problem or accomplishing some end especially by a computer". This step-by-step procedure can be expressed through plain-English descriptions, flowcharts, pseudocode (more of a programmatic description than an actual programming language), or an actual programming language (like C++, Java, or Python). 2. Days in the Month To start, let's start with a simple example, using the months of the year. Here is a simple rhyme you probably learned in school: Thirty days hath September, April, June, and November; All the rest have thirty-and-one, Save February, with twenty-eight days clear, And twenty-nine in each leap year. 3. Turning A Poem Into an Algorithm Starting with the plain-English description, let's take this example and work our way up to a full fledged program, to show the different forms an algorithm might be written in. 3.a. Plain-English Description Plain-English Description The previously introduced poem is a very nice mnemonic, but it isn't an algorithm yet, as it doesn't give step-by-step instructions. Converting into plain-English descriptions could yield the following result: 1. Look at the month's name to see if it is equal to September, April, June, or November. If it is, then there are 30 days on the month. 2. Look at the month's name to see if it is equal to February. If it is and it is also a leap-year, there are 29 days in the month. Otherwise, there are only 28 days in the month. 3. If Step #1 nor Step #2 is true, then there are 31 days in the month. This is now an algorithm as it is a step-by-step instruction to achieve a goal. In this case, our goal is to determine how many days are in a given month. 3.b. Flowchart Now that we have a plain-English description, let's take this a little further and attempt to draw a flowchart diagram. 3.c. Pseudocode Yet another method of describing an algorithm is pseudocode. As its name implies, it isn't an actual programming language. It is just a tool to help describe the inner-workings of a program without getting bogged down in syntax. Here is how pseudocode for the above flowchart might be written: If month name in [September, April, June, November]: return 30 else if month name is February: if is_leap_year(year): return 29 else: return 28 else: return 30 3.d. Programming Language Using descriptions, flowcharts, and pseudocode are extremely helpful in aiding in both the design and communication of an algorithm. However, they are not executable by a computer until they are converted into a programming language. Here are a couple examples in two different programming languages: 3.d.i. Python import calendar def find_days_in_month(month_name, year): if month_name in ["September", "April", "June", "November"]: return 30 elif month_name == "February": if calendar.isleap(year): return 29 else: return 28 else: return 31 3.d.ii. Bash function find_days_in_month { local month_name=$1 local year=$2 case $month_name in "September" | "April" | "June" | "November") return 30; ;; "February") if [$(date -d "Dec 31, $year" +%j) == 366]; then return 29; else return 28; fi ;; *) return 31; ;; esac } 4. Algorithms vs Formulas While preparing this article, it struck me that algorithms and formulas are incredibly similar. Take our "number of days in a month" algorithm that we prepared. Curtis McEnroe has deduced a formula that will accomplish the same thing as our algorithm. 28 + (x + floor(x⁄8)) mod 2 + 2 mod x + 2 floor(1⁄x) Merriam-Webster defines a formula as: "Mathematics : a general fact or rule expressed in letters and symbols". True the above formula is expressed in letters and symbols. However, is it also true that it is step-by-step instructions to achieve the goal of finding the number of days in a month? If so, what really is the difference between the two. Apparently, I'm not the only person to wonder this. I don't have a definitive answer, so instead of providing one, I'll leave you to ponder this on your own :-). 5. Summary In this article, we endeavored to learn what algorithms are. We defined them as a series of step-by-step instructions that accomplish a goal. Then, we took a basic example, created an algorithm, and expressed it as a flowchart, pseudocode, and wrote an implementation in a couple programming languages. Finally, we took a slight detour and we reflected on possible similarities algorithms share with formulas.

Written by:

Peter Naudus
Hire Peter N
I am a seasoned software developer with over 15 years of professional experience. My passion for technology is coupled with honed communication skills and a love for writing.
No Ratings
Hire Peter N

Power your marketing with great writing.

Get Started