TeX - LaTeX Asked by user_777 on May 15, 2021
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
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
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.
Here it is:
Code:
documentclass{article}
usepackage{amsmath}
usepackage[linesnumbered,ruled]{algorithm2e}
begin{document}
begin{algorithm}
SetKwInOut{Input}{Input}
SetKwInOut{Output}{Output}
underline{function Euclid} $(a,b)$;
Input{Two nonnegative integers $a$ and $b$}
Output{$gcd(a,b)$}
eIf{$b=0$}
{
return $a$;
}
{
return Euclid$(b,amod b)$;
}
caption{Euclid's algorithm for finding the greatest common divisor of two nonnegative integers}
end{algorithm}
end{document}
Correct answer by karlkoeller on May 15, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP