algorithm2e学习记录

Intro

reference: algorithm2e – Floating algorithm environment with algorithmic keywords
Algorithm2e 是一个用于编写算法的环境。使一个算法变成一个浮动对象(如图形、表格等)。该包提供了允许您创建不同关键字,并提供一组预定义的关键字;您可以更改关键字的排版。该软件包允许垂直线在算法中分隔指令块,并定义不同种类的算法,例如过程或功能;这些函数的名称可以在文本中重复使用,或者在其他算法中。
文档:http://mirrors.ctan.org/macros/latex/contrib/algorithm2e/doc/algorithm2e.pdf

Basic usage

use package

linesnumbered 添加行号,ruled 排版更好看,longend 为每种语句使用不同的end,如end if, end for

\usepackage[linesnumbered, ruled, longend]{algorithm2e}

assign value

伪代码中常用箭头赋值

view code
\begin{algorithm}
    \SetKwInOut{KwIn}{Input}\SetKwInOut{KwOut}{Output}

    \KwIn{520}
    \KwOut{250}

    $x \leftarrow 520 $;
    \caption{assign value}
    
\end{algorithm}

编译效果如下
image

if-else

一个要注意的细节是,if要搭配end if使用。
在algorithm2e中,if-else有多种命令。
以下几种就能适用于日常的使用了。

  • \eIf{condition}{then block}{else block}
  • \uIf{condition}{then block without end}
  • \uElseIf{elseif block without end}
  • \ElseIf(elseif comment){elseif block}
  • \Else(else comment){else block}
view code
\begin{algorithm}%{H}
    \SetKwInOut{KwIn}{Input}\SetKwInOut{KwOut}{Output}

    \KwIn{score}
    \KwOut{grade}
    \BlankLine

    \tcp{a simple version}
    \eIf(\tcp*[h]{pass of fail}){$score \geq 60$}
        {Pass}{Fail}
    
    \BlankLine
    \tcc{A complex version, more if-else}
    
    \eIf(\tcp*[h]{pass of fail}){$score \geq 60$}
        {
            \uIf{$score \geq 85$}
                {$grade \leftarrow A$}
            \uElseIf {$score \geq 75$}
                {$grade \leftarrow B$}
            \uElseIf {$score \geq 65$}
                {$grade \leftarrow C$}
            \ElseIf {$score \geq 60$}
                {$grade \leftarrow D$}
        }{$grade \leftarrow F$}
    \KwRet{grade}
    \caption{if-else}
\end{algorithm}
编译效果如下

image

loop

这里给出几个常用的loop命令,所有的end condition test都在开头。

  • \For{condition}{text loop}
  • \ForEach{condition}{text loop}
  • \ForAll{condition}{text loop}
  • \While{condition}{text loop}
view code
\begin{algorithm}%{H}
    \SetKwData{Left}{left}\SetKwData{This}{this}\SetKwData{Up}{up}
    \SetKwFunction{IsOdd}{IsOdd}
    \SetKwInOut{KwIn}{Input}\SetKwInOut{KwOut}{Output}

    \KwIn{$a_i$, $0 \leq i \leq n-1$}
    \KwOut{sum of odd numbers minus that of even}
    \BlankLine
    $ sum \leftarrow 0 $
    
    \For{$i \leftarrow 0$ \KwTo $n-1$}{
        \eIf{$\IsOdd(a_i)$}{
            $sum \leftarrow sum +  a_i $
        }{
            $sum \leftarrow sum - a_i$
        }
    }

    \KwRet{sum}
        
    \caption{for loop}
\end{algorithm}

image

view code

\begin{algorithm}
  \SetAlgoLined
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }

  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{while loop}
\end{algorithm}

image

define function

可以用\SetKwFunction{Fn}{Function}来定义函数,并且用命令\Fn来调用Function

view code

\begin{algorithm}
    \SetKwData{Left}{left}\SetKwData{This}{this}\SetKwData{Up}{up}
    \SetKwFunction{quicksort}{quicksort}
    \SetKwFunction{partition}{partition}
    \SetKwFunction{swap}{swap}
    \SetKwInOut{KwIn}{Input}\SetKwInOut{KwOut}{Output}

    \KwIn{$arr_i$, $0 \leq i \leq n-1$ - array to be sorted}
    \KwOut{$arr_i$, $0 \leq i \leq n-1$ - sorted array}
    \BlankLine

    \SetKwProg{fn}{Function}{:}{}
    \fn{\quicksort{arr, low, high}}{
        \If{low < high}{
            pivot\_idx $\leftarrow$ partition(arr, low, high)
            
            \quicksort(arr, low, pivot\_idx-1)
            
            \quicksort(arr, pivot\_idx+1, high)
        }
    }
    \textbf{End Function}
    
    \BlankLine
    \fn{\partition{arr, low, high}}{
        $pivot$ $\leftarrow$ arr[high]
        
        $i$ $\leftarrow$ low - 1
        
        \For{$j$ $\leftarrow$ low to high-1}{
            \If{arr[$j$] $\leq$ $pivot$}{
                $i$ $\leftarrow$ $i$ + 1
                
                \swap(arr[$i$], arr[$j$])
            }
        }
        
        swap(arr[$i$+1], arr[high])
        
        \Return $i$
    }
    \textbf{End Function}
    \BlankLine

    \quicksort(arr, 0, n-1)

    \caption{QuickSort}
\end{algorithm}

编译效果如下

image

posted @ 2022-12-09 11:35  Cisco_coco  阅读(800)  评论(0编辑  收藏  举报