Skip to main content
Engineering LibreTexts

5.2: More Examples of Algorithms

  • Page ID
    112255
  • \( \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}\)

    Example

    Write an algorithm that returns the index of the first occurrence of a specific character in a string (a sequence of characters): s1, s2, ..., sn

    Here is the word "index" is referring to the position of a character in a sequence. For example, the index of '&' in the sequence "abcd&dcba" is 5

    Algorithm: Character Search

    This algorithm is used for finding the index of the first occurrence of a specific character in a string

    Input: a string of character s = s1, s2, ..., sn and a character ch

    Output: k (an integer that is the index of the first occurrence of ch in the string s) If the character ch is not in the string s, then the output is 0;

    getIndex(s, ch)
    {
    for k=1 to n
    if(sk==ch)
    return k;
    return 0;
    }

    Example

    Write an algorithm that returns the index of the last occurrence of a specific character in a string (a sequence of characters): s1, s2, ..., sn

    Algorithm: Find the index of the last occurrence of a specific character in a string

    This algorithm is used for finding the index of the last occurrence of a specific character in a string

    Input: a string of character s = s1, s2, ..., sn and a character ch

    Output: index (an integer that is the index of the last occurrence of ch in the string s) If the character ch is not in the string s, then index=0;

    getIndex(s, ch)
    {
    int index
    for k=1 to n
    if(sk==ch){
    index=k;
    }
    return index;
    }

    Examples presented above are algorithms of simple programming tasks. It seems trivial if we need to write those algorithms for the tasks. Unfortunately, it is not always the case. Let's study an algorithm that is slightly more difficult then the previous two examples.

    Searching Algorithm

    One of major computer programming task is searching. Examples are searching a student record from a school data file, searching a website from the internet, searching a book from a library and so on. All of those tasks can be stated as a searching problem. Here we will discuss an algorithm to solve a tex-searching problem.

    Example

    Suppose we are given a text document and look for the first occurrent of a specific word. Instead of coding a program directly, we write an algorithm that provides a solution to this searching problem.

    Algorithm: Word Search

    This algorithm searches for an occurrence of a word W in a text document D. It returns the smallest index i such that W occurs in D starting at index i. If the word W does not occur in D, it returns 0.

    Input: W (indexed from 1 to m), m, D (indexed from 1 to n), n

    Output: i

    1 word_search(W, m, D, n)
    2 {
    3 for i=1 to n-m+1 {
    4 j=1
    5 while(Di+j-1==Wj){
    6 j++;
    7 if(j>m)
    8 return i
    9 }
    10 }
    11 return 0
    12 }

    If we are in the real world and facing such programming task. That is all we will get as a guideline for coding the program. It will leave us to figure out the logic of the algorithm. Since we are now in the classroom situation and we are learning it, we will provide further assistance to explain the logic of this algorithm.

    1. All the characters in the document D are indexed as Di , with i=1, 2, 3, ..., n and all the characters in the word W are indexed as Wj , with j=1, 2, 3, ..., m
    2. Inputs of the function are: the text document. It may be an array with n elements or a Linked list with n nodes or some other types of data structures; the length of the array, which is n; the word that we are looking for. It may also be an array with m elements or a linked list with m nodes or some other types of data structures; the length of the array, which is m. So the inputs are D, n, W, m
    3. Output: i, which is the index of the first character of the word when it first occurs in the document.
    4. Line 1 specify the name of the function as well as its inputs. The function has total of four inputs: The word that we are search for, the length of the word, the document that we will be searching, the length of the document, which is the total number of characters in the document.
    5. From line 3 to line 9, it is a nested looping statement with an outer for loop and an inner while loop with outer looping variable i and an inner looping variable j
    6. The outer loop varies the value of i from 1 to n-m+1 because the number of characters in the word is m, if the word is in the document, then the index of the first character in the word can not be greater than n-m+1. For example, if the document has 100 characters, then n=100. If the word that we are searching for has 5 characters, them m=5. So, the largest possible value of i can not be greater than 100-5+1, which is 96. Since i is the index of the first character in the word, and word has 5 characters, if i=97, then we won't have the full word occurs in the document.
    7. The nested looping statement begins with D1 , which is the first character in the document. The inner while loop compare this character with each character in the word Wj with j=1, 2,...,m. If there is a match, then compare with the next character in the word (j=j+1). If there is a mismatch, inner loop stop, increase the value i (which means moving to the next character in the document) and then repeat the inner loop (which is comparing the next character in the document with each character in the word as we did in the previous step)
    8. If one of the character and onward four consecutive characters all match, we would have j>m, then the function returns i, which will be the index of the first character of the word when it first occurs in the document.
    9. If the algorithm never return i, then it means the document does not contain the word, so the function returns 0

    Hope those explanations help you better understand the logic of the algorithm. You can also use some sample inputs to trace the algorithm.


    5.2: More Examples of Algorithms is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?