Main content
AP®︎/College Computer Science Principles
Expressing an algorithm
We can express an algorithm many ways, including natural language, flow charts, pseudocode, and of course, actual programming languages.
Natural language is a popular choice, since it comes so naturally to us and can convey the steps of an algorithm to a wide audience. When we're developing algorithms, we are often working both with people who know programming and those who don't—but they all know natural language.
However, natural language has its drawbacks. It has a tendency to be ambiguous and too vaguely defined, since it has no imposed structure. That makes it difficult for others to follow the algorithm and feel confident in its correctness. Flow charts and pseudocode are more structured formats that can more precisely express an algorithm, and are popular with computer scientists and programmers.
Let's try out the more structured formats by expressing the Pig Latin algorithm from the previous article.
Flow charts
A more formal way to express an algorithm is with a flow chart, a diagram with boxes connected by arrows.
To start simple, here's a flow chart for the basic version of the Pig Latin algorithm:
Each rectangle represents a step in the sequence, and the arrows flow from one step to the next.
This next flow chart is for the improved algorithm and uses a diamond to represent the selection phase:
Finally, this flow chart visualizes the complete algorithm with iteration:
Expressing an algorithm in a flow chart allows us to visualize the algorithm at a high level, plus it forces us to think very carefully about sequencing and selection. Which arrow goes to what node? Are there missing arrows? Those are the kinds of valuable questions that can come up while translating an algorithm into a flow chart.
Pseudocode
Ultimately, most algorithms become code that actually runs on a computer. Before that happens, programmers often like to express an algorithm in pseudocode: code that uses all the constructs of a programming language, but doesn't actually run anywhere.
Here's the Pig Latin algorithm written in the style of AP CSP pseudocode:
FOR EACH word IN words
{
APPEND(word, "-")
letter ← FIRST_LETTER(word)
IF (IS_VOWEL(letter)) {
APPEND(word, "yay")
} ELSE {
APPEND(word, letter)
APPEND(word, "ay")
REMOVE_FIRST(word)
}
}
Every programmer writes pseudocode differently, since there is no official standard, so you may run into pseudo-code that looks very different.
Expressing an algorithm in pseudocode helps a programmer think in familiar terms without worrying about syntax and specifics. It also gives computer scientists a language-independent way to express an algorithm, so that programmers from any language can come along, read the pseudo-code, and translate it into their language of choice.
Programming languages
Once we've planned our algorithm, whether in natural language, flow charts, pseudocode, or a combination, it's time to turn it into running code.
We'll translate the Pig Latin algorithm to JavaScript, since that's our language of choice on Khan Academy. We can use a
for
loop for the repetition, an if
/else
for the selection, and then a mix of string and array operations for the steps.An algorithm can be translated into any general purpose programming language. For proof, just check out RosettaCode.org, a wiki that lists hundreds of algorithms translated to over 700 languages.
Software is written in a variety of programming languages, depending on what it does and who's making it. Khan Academy currently uses four different languages for different parts of the codebases. Fortunately, we can use the same algorithms everywhere!
🙋🏽🙋🏻♀️🙋🏿♂️Do you have any questions about this topic? We'd love to answer—just ask in the questions area below!
Want to join the conversation?
- Why does Khan Academy use 4 different programming languages? Why not consolidate to one language?(3 votes)
- There are different reasons for each language. We use JavaScript for the frontend, since JavaScript is the language understood by web browsers. On the backend, we currently use a mix of Python and Kotlin. However, we are moving the backend over to Go, for performance reasons. So we will have a mix of those three languages for a while, since there is a lot of code to upgrade!
(Also, our data analytics team uses a mix of Python and R, two languages that have a lot of available libraries for data processing)(16 votes)
- Algorithms are what are used to create path-finding, correct?(2 votes)
- Yes, that's correct, there are various algorithms that can be used for path-finding.
Here's a fun visualization of different algorithms:
https://qiao.github.io/PathFinding.js/visual/
This article looks like a good introduction as well:
https://www.redblobgames.com/pathfinding/a-star/introduction.html
...It's a fun thing to try out in a ProcessingJS game!(9 votes)
- bro what kind of goofy language is pig latin on god(6 votes)
- Pig Latin -
If a word starts with a consonant and a vowel, put the first letter of the word at the end of the word and add "ay."
If a word starts with two consonants move the two consonants to the end of the word and add "ay."
If a word starts with a vowel add the word "way" at the end of the word.
to help anyone unfamiliar with the concept of pig latin(4 votes) - Could you post some links to information on using algorithms for creating artificial intelligence?(2 votes)
- What are the "STORE words" & "FOR word IN words" steps in the flowchart example for?(1 vote)
- Is 4 the limited number of languages?(1 vote)
- No according to the article, Khan Academy uses four different languages for different parts of the site. That's specific to this site, there are hundreds of programming languages. But you can take algorithm and implement it in hundreds of different programming languages, in that way algorithms are universal. Although some languages are better at some algorithms than others.(1 vote)
- did somebody knew how can dowmload the java?(1 vote)
- What does append mean?(0 votes)
- Append means to add to the end. For example, if we append 4 to the list [1, 2, 3], we would get [1, 2, 3, 4].(2 votes)
- What does !== -1 mean (it's in the code example)?(0 votes)
- "vowels.indexOf(char.toLowerCase()) !== -1" compares whether "vowels.indexOf(char.toLowerCase())" does not equal -1. This would be the opposite of "vowels.indexOf(char.toLowerCase()) === -1" which would determine if the expression does equal -1.(4 votes)