[Tex/LaTex] Inserting multiple columns in algorithm

algorithmicalgorithms

I am currently working on runtime in my algorithm. I have currently set the algorithm up, and it looks like the following:

My Algorithm

I need to insert 2 new columns beside the current text. Each column should be able to hold some text, so that it would look like the following:

enter image description here

How would I go around doing this?

My current code can be seen below:

\begin{algorithm}
\caption{Insertion-Sort køretid}\label{AlgRun}
\begin{algorithmic}[1]
    \Procedure{insertion-sort}{$A,n$}
        \State $inversions = 0$
        \State \textit{L}[0..\textit{n} - 1] be a new array.
        \For{q=0}{n-1}
            \State L[q] = A[q]
        \EndFor
        \For{j=1}{n-1}
            \State $key = L[j]$
            \State $i = j - 1$
            \While{$ i \geq 0$ \textbf{and} $L[i] > key$}
                \State L[ i + 1] = L[ i ]
                \State $j = i - 1$
                \State $inversions = inversions + 1$
            \EndWhile
            $L[i + 1] = key$
        \EndFor
        \Return{inversions}
    \EndProcedure
\end{algorithmic}
\end{algorithm}

Best Answer

The algorithmicx package provides a \Comment command which does what you need, and is customizable. In this example I've created a macro with two arguments, one for each column of the annotation. Then each line gets inserted with a \Comment command. I've also redefined the standard \Comment format macro (\algorithmiccomment) with the \algrenewcommand macro to remove the arrow that shows up by default.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{array}
\newcolumntype{L}{>{$}l<{$}}
\newcommand\cpair[2]{%
\begin{tabular}{LL}#1 & #2\end{tabular}}
\algrenewcommand{\algorithmiccomment}[1]{\hfill#1}
\begin{document}
\begin{algorithm}
\caption{Insertion-Sort køretid}\label{AlgRun}
\begin{algorithmic}[1]
    \Procedure{insertion-sort}{$A,n$}   \Comment \cpair{c_1}{n=0}
        \State $inversions = 0$         \Comment \cpair{c_1}{n=1}
        \State \textit{L}[0..\textit{n} - 1] be a new array.
        \For{q=0}{n-1}
            \State L[q] = A[q]
        \EndFor
        \For{j=1}{n-1}
            \State $key = L[j]$
            \State $i = j - 1$
            \While{$ i \geq 0$ \textbf{and} $L[i] > key$}
                \State L[ i + 1] = L[ i ]
                \State $j = i - 1$
                \State $inversions = inversions + 1$
            \EndWhile
            $L[i + 1] = key$
        \EndFor
        \Return{inversions}
    \EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}

output of code