如何像 CLRS(演算法簡介)中那樣用列排版演算法?

如何像 CLRS(演算法簡介)中那樣用列排版演算法?

演算法簡介,作者:Thomas Cormen,有一些類似此圖中的演算法。一欄用於代碼,一欄用於成本,一欄用於時間。

我搜尋了很多,但沒有找到如何做。

在此輸入影像描述

答案1

如果我不使用clrscode.

\documentclass[11pt]{article}

\usepackage[noend]{algorithmic}

\newcommand{\TITLE}[1]{\item[#1]}
\renewcommand{\algorithmiccomment}[1]{$/\!/$ \parbox[t]{4.5cm}{\raggedright #1}}
% ugly hack for for/while
\newbox\fixbox
\renewcommand{\algorithmicdo}{\setbox\fixbox\hbox{\ {} }\hskip-\wd\fixbox}
% end of hack
\newcommand{\algcost}[2]{\strut\hfill\makebox[1.5cm][l]{#1}\makebox[4cm][l]{#2}}

\begin{document}
\begin{algorithmic}[1]
  \TITLE{\textsc{Insertion-Sort}$(A)$}
    \algcost{\textit{cost}}{\textit{times}}
  \FOR{$j=2$ \TO $A.\mathit{length}$
    \algcost{$c_1$}{$n$}}
  \STATE $\mathit{key} = A[j]$
    \algcost{$c_2$}{$n-1$}
  \STATE \COMMENT{Insert $A[j]$ to the sorted sequence $A[1..j-1]$}
    \algcost{$0$}{$n-1$}
  \STATE $i = j-1$
    \algcost{$c_4$}{$n-1$}
  \WHILE{$i>0$ \AND $A[i]>key$
    \algcost{$c_5$}{$\sum_{j=2}^{n} t_j$}}
  \STATE $A[i+1]= A[i]$
    \algcost{$c_6$}{$\sum_{j=2}^{n} (t_j-1)$}
  \STATE $i = i-1$
    \algcost{$c_7$}{$\sum_{j=2}^{n} (t_j-1)$}
  \ENDWHILE
  \STATE $A[i+1] = \mathit{key}$
    \algcost{$c_8$}{$n-1$}
  \ENDFOR
\end{algorithmic}
\end{document}

請注意以下幾點:

  1. 可能需要調整三個寬度(以公分為單位)。

  2. for和語句的成本while位於括號內,它們與右括號之間不能有任何空格。這有點脆弱。 forif和所有其他區塊語句相同。

  3. 第 3 行的成本顯示在兩行中第一行的旁邊,而不是第二行(如原來所示)。這樣比較容易,但我想我也比較喜歡這樣。

這個答案採取與 Jubobs 的答案相反的路線,即使用該clrscode包。如果您更喜歡使用clrscode,我相信您會找到正確使用選項卡附加列的方法|>,儘管它沒有很好的記錄。此外,如果您希望您的演算法與書中的演算法完全相同,那麼出於其他原因,它也可能更可取。


結果

答案2

(僅部分回答)

科門用他的clrscode包裹CLRS 的第二版,但它的「增強」版本,稱為clrscode3e,對於第三版,其中插入排序截圖中的演算法已被採用。有關更多詳細信息,請參閱

排版演算法的程式碼(但是沒有「成本」和「時間」欄)可在第 6 頁找到clrscode3e文件。後者沒有提及如何排版列。此外,該包的源代碼似乎沒有為此提供任何(未記錄的)機制。

然而,Cormen的codebox環境是基於tabbing環境的;也許那裡有什麼可做的,但我不太熟悉tabbing...

在此輸入影像描述

\documentclass{article}

\usepackage{clrscode3e}

\begin{document}

\begin{codebox}
\Procname{$\proc{Insertion-Sort}(A)$}
\li \For $j \gets 2$ \To $\attrib{A}{length}$
\li     \Do
            $\id{key} \gets A[j]$
\li         \Comment Insert $A[j]$ into the sorted sequence
                $A[1 \twodots j-1]$.
\li         $i \gets j-1$
\li         \While $i > 0$ and $A[i] > \id{key}$
\li             \Do
                    $A[i + 1] = A[i]$
\li                 $i \gets i-1$
                \End
\li         $A[i+1] \gets \id{key}$
        \End
\end{codebox}

\end{document}

答案3

有 CLRS 包,代號為“clrscode3e”,該包的描述以及文件.sty可以在網站中找到:http://www.cs.dartmouth.edu/~thc/clrscode/,您可以輕鬆下載該.sty文件並閱讀說明。

\documentclass{article}

\usepackage{clrscode3e}

\begin{document}

\begin{codebox}
\Procname{$\proc{Insertion-Sort}(A)$}
\li \For $j \gets 2$ \To $\attrib{A}{length}$
\li     \Do
            $\id{key} \gets A[j]$
\li         \Comment Insert $A[j]$ into the sorted sequence
                $A[1 \twodots j-1]$.
\li         $i \gets j-1$
\li         \While $i > 0$ and $A[i] > \id{key}$
\li             \Do
                    $A[i + 1] = A[i]$
\li                 $i \gets i-1$
                \End
\li         $A[i+1] \gets \id{key}$
        \End
\end{codebox}
\end{document}

相關內容