I would like to know how to format a pseudocode algorithm like the one shown in the picture below. I would like to see an example of Tex/Latex code that would mimic the style, formatting and design of the pseudocode illustrated on this picture. I know how to write simple pseudocode algorithms, but i don't know how to
- Align the pseudocode with an \item "Some text.."
- How to write a pseudocode with Input and Output exactly below the procedure/function so that they are not numbered and aligned with procedure/function
- How to use the block braces in form of "vertical lines"
My attempt
\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{graphicx}
\usepackage[colorinlistoftodos]{todonotes}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{geometry}
\geometry{
a4paper,
total={210mm,297mm},
left=20mm,
right=20mm,
top=20mm,
bottom=20mm,
}
\begin{document}
\begin{enumerate}
\item Some text goes here . . .
\begin{algorithm}
\caption{Merge Sort}
\begin{algorithmic}[1]
\Function{Merge}{$A,p,q,r$}\Comment{Where A - array, p - left, q - middle, r - right}
\State ${n_1} = q - p + 1$
\State ${n_2} = r - q$
\State Let $L[1 \ldots {n_1} + 1]$ and $R[1 \ldots {n_2} + 1]$ be new arrays
\For{$i = 1$ to ${n_1}$}
\State $L[i] = A[p + i - 1]$
\EndFor
\For{$j = 1$ to ${n_2}$}
\State $R[i] = A[q + j]$
\EndFor
\State $L[{n_1} + 1] = \infty $
\State $R[{n_2} + 1] = \infty $
\State $i = 1$
\State $j = 1$
\For{$k = p$ to $r$}
\If {$L[i] < R[j]$}
\State $A[k] = L[i]$
\State $i = i + 1$
\ElsIf {$L[i] > R[j]$}
\State $A[k] = R[j]$
\State $j = j + 1$
\Else
\State $A[k] = - \infty$ \Comment{We mark the duplicates with the largest negative integer}
\State $j = j + 1$
\EndIf
\EndFor
\EndFunction
\end{algorithmic}
\end{algorithm}
\end{enumerate}
\end{document}
My result
Comments
- As you can see i don't know how to align the algorithm with the itemized text.
- I don't know how to place Input and Output words below the function, so that they are not numbered and aligned with a function.
- I like more the style of vertical line block, rather than just condition:end.
I'm new to writing pseudocode algorithms with Latex, but i suspect the style and formatting i'm looking for is in the package algorithm2e. Can someone show me how to achieve the following result:
I want to learn to write pseudocode algorithm in the same style as in the picture above.
algorithm2e
package. – Werner Oct 5 '14 at 4:48