\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{microtype}

\usepackage[vlined,linesnumbered]{algorithm2e}
\DontPrintSemicolon

% function/procedure keywords
\SetKwProg{Fn}{function}{}{}
\SetKwProg{Proc}{procedure}{}{}

% some other auxiliary keywords
\SetKw{From}{from}
\SetKw{DownTo}{downto}
\SetKw{And}{and}

% function names
\SetKwFunction{Swap}{swap}
\SetKwFunction{Max}{maximum}
\SetKwFunction{SelectSort}{select\_sort}
\SetKwFunction{InsertLast}{insert\_last}
\SetKwFunction{InsertSort}{insert\_sort}
\SetKwFunction{BubbleSort}{bubble\_sort}
\SetKwFunction{Bubble}{bubble}

\begin{document}

\begin{algorithm}[H]
\Fn{\Max{$A[1..n]$}}{
    $m \gets 1$\;
    \For{$i$ \From 2 \KwTo $n$}{
        \If{$A[m] < A[i]$}{
            $m \gets i$\;
        }
    }
    \Return $m$\;
}
\end{algorithm}

\begin{algorithm}[H]
\Fn{\Max{$A[1..n]$}}{
    $m \gets 1$\;
    $i \gets 2$\;
    \While{$i \le n$}{
        \If{$A[m] < A[i]$}{
            $m \gets i$\;
        }
        $i \gets i + 1$\;
    }
    \Return $m$\;
}
\end{algorithm}

\begin{algorithm}[H]
\Proc{\SelectSort{$A[1..n]$}}{
    \For{$i$ \From $n$ \DownTo 2}{
        $m \gets \Max{$A[1..i]$}$\;
        \Swap{$A[i]$, $A[m]$}\;
    }
}
\end{algorithm}

\begin{algorithm}[H]
\Proc{\SelectSort{$A[1..n]$}}{
    $i \gets n$\;
    \While{$i \ge 2$}{
        $m \gets \Max{$A[1..i]$}$\;
        \Swap{$A[i]$, $A[m]$}\;
        $i \gets i - 1$\;
    }
}
\end{algorithm}

\begin{algorithm}[H]
\Proc{\InsertSort{$A[1..n]$}}{
    \For{$i$ \From 2 \KwTo $n$}{
        $k \gets i$\;
        \While{$k > 1$ \And $A[k-1] > A[k]$}{
            \Swap{$A[k-1]$, $A[k]$}\;
            $k \gets k - 1$\;
        }
    }
}
\end{algorithm}

\begin{algorithm}[H]
\Proc{\InsertSort{$A[1..n]$}}{
    \For{$i$ \From 2 \KwTo $n$}{
        \InsertLast{$A[1..i]$}\;
    }
}
\Proc{\InsertLast{$A[1..i]$}}{
    $k \gets i$\;
    \While{$k > 1$ \And $A[k-1] > A[k]$}{
        \Swap{$A[k-1]$, $A[k]$}\;
        $k \gets k - 1$\;
    }
}
\end{algorithm}

\begin{algorithm}[H]
\newcommand{\last}{\mathit{last}}
\Proc{\BubbleSort{$A[1..n]$}}{
    $\last \gets n$\;
    \While{$\last > 1$}{
        $\last \gets \Bubble{$A[1..\last]$}$\;
    }
}
\Fn{\Bubble{$A[1..k]$}}{
    $\last \gets 1$\;
    \For{$i$ \From 1 \KwTo $k - 1$}{
        \If{$A[i] > A[i+1]$}{
            \Swap{$A[i]$, $A[i+1]$}\;
            $\last \gets i$\;
        }
    }
    \Return $\last$\;
}
\end{algorithm}

\end{document}
