我目前正在研究算法的运行时。我目前已设置算法,它如下所示:
我需要在当前文本旁边插入 2 个新列。每列应该能够容纳一些文本,以便它看起来如下所示:
我该如何做这件事?
我的当前代码如下所示:
\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}
答案1
该algorithmicx
包提供了一个\Comment
命令,它可以执行您需要的操作,并且可以自定义。在此示例中,我创建了一个带有两个参数的宏,注释的每一列一个参数。然后每行都插入一个\Comment
命令。我还用宏重新定义了标准\Comment
格式宏(\algorithmiccomment
)\algrenewcommand
,以删除默认显示的箭头。
\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}