Writing algorithm in LATEX

Packages may be used: algorithmic, algorithmicx, algorithm2e

Example of each package:

Example of algorithmic

\begin{algorithmic}
    \IF{some condition is true}
        \STATE do some processing
    \ELSIF{some other condition is true}
        \STATE do some different processing
    \ELSE
        \STATE do the default actions
    \ENDIF
\end{algorithmic}

Example of algorithmicx

\begin{algorithm}
    \caption{Euclid’s algorithm}
    \label{euclid}
    \begin{algorithmic}[1] % The number tells where the line numbering should start
        \Procedure{Euclid}{$a,b$} \Comment{The g.c.d. of a and b}
            \State $r\gets a \bmod b$
            \While{$r\not=0$} \Comment{We have the answer if r is 0}
                \State $a \gets b$
                \State $b \gets r$
                \State $r \gets a \bmod b$
            \EndWhile\label{euclidendwhile}
            \State \textbf{return} $b$\Comment{The gcd is b}
        \EndProcedure
    \end{algorithmic}
\end{algorithm}

Example of algorithm2e

\begin{algorithm}[H]
 \KwData{this text}
 \KwResult{how to write algorithm }
 initialization\;
 \While{not at end of this document}{
  read current\;
  \eIf{understand}{
   go to next section\;
   current section becomes this one\;
   }{
   go back to the beginning of current section\;
  }
 }
 \caption{How to write algorithms}
\end{algorithm}

Why I prefer algorithm2e?

Basic algorithm2e package example

\documentclss{article}
\usepackage{algorithm2e}
\begin{document}
  \begin{algorithm}[H]
        <--- your algorithm code goes here --->
  \end{algorithm}
\end{document}

Input and output

Input: KwIn

output: KwOut

\documentclass{article}
\usepackage{algorithm2e}
\begin{document}
\begin{algorithm}[H]

\KwIn{A array}
\KwOut{Write here the result }


\end{algorithm}
\end
▶︎
all
running...

if block

\documentclass{article}
\usepackage{algorithm2e}
\begin{document}
\begin{algorithm}[H]
\SetAlgoLined
\KwIn{A array}
\KwOut{Write here the result }

\If{condition}
{
      instructions1\;
      instructions2\;
 }

\end{algorithm}
\end{document}
▶︎
all
running...

If-else block

\documentclass{article}
\usepackage{algorithm2e}
\begin{document}
\begin{algorithm}[H]
\SetAlgoLined
\KwIn{A array}
\KwOut{Write here the result }

\eIf{condition}
{
      instructions1\;
      instructions2\;
 }
 {
     instructions3\;
 }

\end{algorithm}
\end{document}
▶︎
all
running...

while loop:

\documentclass{article}
\usepackage{algorithm2e}
\begin{document}
\begin{algorithm}[H]
\SetAlgoLined
\KwIn{A array}
\KwOut{Write here the result }

\While{While condition}
{
  instructions1\;
  instructions2\;
}

\end{algorithm}
\end{document}
▶︎
all
running...

for loop:

\documentclass{article}

\usepackage{algorithm2e}
\begin{document}
\begin{algorithm}[H]


\For{condition}{
statemet 1\;
}
\end{algorithm}
\end{document}
▶︎
all
running...

Example of bigger number among 3 numbers

\documentclass{article}

\usepackage{algorithm2e}
\begin{document}
\begin{algorithm}[H]
\KwIn{Three numbers $a,b,c$}
\KwOut{Largest number}
\eIf{$a>b$}
  {
    \eIf{$a>c$}
    {
      $largest\leftarrow a$\;
    }
    {
      $largest\leftarrow c$\;
    }
  }
  {
    \eIf{$b>c$}
    {
      $largest\leftarrow b$\;
    }
    {
      $largest\leftarrow c$\;
    }
  }
\end{algorithm}
\end{document}
▶︎
all
running...

\documentclass{article}

\usepackage{algorithm2e}
\begin{document}
\begin{algorithm}[H]
\KwIn{Lower bound $l \leftarrow 0$, Uper bound $u \leftarrow 100$  }
\KwOut{Print all even numbers}
\For{$i$ in range $(l,b)$}
  {
    \If{mod(i, 2)=0}
      {
        print i\;
      }
  }
\end{algorithm}
\end{document}
▶︎
all
running...