Pseudocode with algorithmicx
The algorithm package provides a numbered, captioned float. The algpseudocode style provides readable commands for pseudocode. Add both in the preamble.
\usepackage{algorithm}
\usepackage{algpseudocode}
A complete example: binary search
\begin{algorithm}[tbp]
\caption{Binary search}\label{alg:binary-search}
\begin{algorithmic}[1]
\Require Sorted array $A[1..n]$, target $x$
\Ensure Index of $x$, or $-1$ if it is absent
\State $low \gets 1$, $high \gets n$
\While{$low \leq high$}
\State $mid \gets \lfloor (low+high)/2 \rfloor$
\If{$A[mid] = x$}
\State \Return $mid$
\ElsIf{$A[mid] < x$}
\State $low \gets mid+1$
\Else
\State $high \gets mid-1$
\EndIf
\EndWhile
\State \Return $-1$
\end{algorithmic}
\end{algorithm}
Algorithm~\ref{alg:binary-search} halves the search interval each iteration.Useful pseudocode commands
\Requireand\Ensuredescribe inputs and outputs.\Stateadds a statement; optional[1]onalgorithmicenables line numbers.\If,\ElsIf,\Else, and\EndIfexpress branches.\For,\ForAll,\While, and\Repeat/\Untilexpress loops.\Procedure,\Function, and\Returnhelp describe reusable steps.
\For{$i \gets 1$ to $n$}
\State process $A[i]$
\EndFor
\Function{Square}{$x$}
\State \Return $x^2$
\EndFunction
Do not mix syntax from different pseudocode packages. For example, algorithmic and algpseudocode have different command styles. If your journal or conference provides a template, use the packages and conventions it specifies.