Skip to main content
Engineering LibreTexts

5.1: Introduction to Algorithm

  • Page ID
    112254
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    We have frequently used the word algorithm in our programming class to mean the idea, or the method of solving a programming problem. More precisely, an algorithm is a step-by-step method of solving some problem. In computer science, algorithm typically refers to a solution that can be executed by a computer.

    Algorithms typically have the following characteristics:

    • An algorithm normally requires some input data, which means an algorithm should specify what kind of data it works on
    • An algorithm normally produces an output. Such output can be multiple data values, multiple data types. It can also be a display
    • The steps in an algorithm are precisely stated
    • The intermediate results of each step of execution are unique and are determined only by the inputs and results of the preceding steps. It means, if an algorithm receives the same set of inputs, it should produce the same set of outputs.
    • The output produced by an algorithm should be correct
    • The algorithm should be applied to a set of inputs. In other words, an algorithm should not just work for a specific input.

    But, what exactly an algorithm is? Let's take a look at an example.

    Example

    Suppose we are given a program task: "Write a user-defined function that receives three numbers and returns the maximum of those three numbers the function receives."

    Before we proceed, we need to note:

    1. We don't have to write an algorithm for each programming task before the coding. It is just like we don't have to have speech notes before we give a speech. But, preparing notes for a speech will definitely help us to give a better speech.
    2. We may have different ways of solving a programming problem. Thus, there may be different algorithms for a single programming task.

    The programming task given in this example can be accomplished in many different ways, therefore, we can write different algorithms for this programming task. Here we only provide one of them which is considered as the best.

    Algorithm of Finding the Maximum of Three Numbers

    Input: three numbers a, b, and c

    Output: The largest of a, b, and c

      1. large = a
      2. if b > large, then large = b;
      3. if c > large, then large = c;

    return large

    Of course, we don't really need to write such algorithm to code the function. But, by studying the algorithm of this simple programming task which we already know how to code, we will understand what an algorithm is and how it should be written.

    Writing an algorithm is like writing a program in normal language. But, not exactly. It is more like to write a plan for a task.

    Writing an algorithm is providing a solution to a programming problem. One of important benefits of writing an algorithm as the solution is the algorithm we provide is not programming language specific. It can be used to implement the programming solution with different programming languages. For example, if we provide the solution of this task with C++ code directly, then the people who are using python may not understand what we are talking about.

    The idea of above algorithm is to inspect the numbers one by one and copy the largest value seen into a variable large. At the conclusion of the algorithm, the value of the variable large will then be equal to the largest of the three numbers.

    It is easy to understand this algorithm because we already know how to solve this problem before we see the algorithm. For some unfamiliar programming tasks, even someone provides the solution in the form of an algorithm, it may still be difficult to understand what the algorithm is talking about. To be a good programmer, following two abilities are essential:

    1. Understand an algorithm written by someone else
    2. To be able to write an algorithm for the solution of a programming problem.

    One of the common way to study an algorithm is to execute the algorithm for some specific values of the variables in the algorithm. Such simulation is called a trace. In the process of the tracing, we record the values of each variable at each step.

    Here, we show how to trace the algorithm presented in this example.

    First suppose that

    a = 1, b=5, c = 3

    At line 1: we set the variable large to a, so the value of the variable large now is 1

    At line 2; we compare the value of the next number, b, with the value of the variable large. If b > large, then change the value of large to b, otherwise, do nothing. Since b = 5, large =1, we change the value of large to 5

    At line 3: we compare the value of the third number, c, with the value of the variable large. If b > large, then change the value of large to c, otherwise, do nothing. Since c = 3, large =5, we don't do anything.

    At this point, we can see the algorithm ensures the value of the variable large holds the largest numbers of a, b, and c.

    For an unfamiliar algorithm or a more complicated algorithm, we often need to trace the algorithm several times with different sets of input data.

    Although ordinary language is sometimes adequate to specify an algorithm, most programmers and computer scientists prefer a so called pseudocode because of its precision, structure, and universality. Pseudocode is so named because it resembles the actual code of computer languages such as C++, Java.

    Here we provide the pseudocode version of the algorithm we presented in this example:

    Algorithm of Finding the Maximum of Three Numbers (written in pseudocode)

    This algorithm finds the largest of the numbers a, b, and c

    Input: three numbers a, b, and c

    Output: large (the largest of a, b, and c)

    1. maxThree(a, b, c){
    2. large = a;
    3. if(b>large) //if b is greater than large, update large
    4. large=b
    5. if(c>large) //if c is greater than large, update large
    6. large=c;
    7. return large
    8. }

    As the pseudocode of the algorithm, here we

    • Given a title to the algorithm
    • Provide a brief description of the algorithm
    • List the input and outputs of the algorithm
    • Provide a function that contains step by step instructions
    • for convenience, label each line of the pseudocode.

    Example

    Programming Task: "Find the largest number in a given sequence" Now, let's write an algorithm for this task.

    Algorithm Title: Finding the Maximum Value in a Sequence

    Algorithm Description: This algorithm finds the largest of the numbers s1, s2, ..., sn

    List of Inputs and output: The sequence s, which represents s1, s2, ..., sn and size or the length of the sequence, n

    Function:

    getMax(s, n)
    {
    large=s1
    for k=2 to n
    if (sk>large)
    large=sk
    return large
    }

    If we eliminate those labels and descriptions, the algorithm can be trimmed down to

    Finding the Maximum Value in a Sequence

    This algorithm finds the largest of the numbers s1, s2, ..., sn

    Input: s, n

    Output: large

    getMax(s, n)
    {
    large=s1
    for k=2 to n
    if (sk>large)
    large=sk
    return large
    }

    This algorithm is using the same logic as the one we used in the previous example. But this algorithm can be used for solving more general problem: "Find the largest value from any sequence of data"


    5.1: Introduction to Algorithm is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?