Skip to content

Commit

Permalink
add Binary Search (#120)
Browse files Browse the repository at this point in the history
Co-authored-by: ramona.burger <[email protected]>
  • Loading branch information
rmnbrgr and ramona.burger authored Oct 23, 2020
1 parent 0711112 commit 53592e7
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Pseudocode/Searching/Binary Search/SourceCode.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
% Set the Page Layout
\documentclass[12pt]{article}
\usepackage[inner = 2.0cm, outer = 2.0cm, top = 2.0cm, bottom = 2.0cm]{geometry}

% Package to write pseudo-codes
\usepackage{algorithm}

% Don't Remove the 'end' at the end of the algorithm
\usepackage{algpseudocode}

% Manually remove the 'end' for some sections
\algtext*{EndIf}
\algtext*{EndFor}

% Define Left Justified Comments
\algnewcommand{\LeftComment}[1]{\Statex \(\triangleright\) #1}

% Remove the Numbering of the Algorithm
\usepackage{caption}
\DeclareCaptionLabelFormat{algnonumber}{Algorithm}
\captionsetup[algorithm]{labelformat = algnonumber}

% Define the command for a boldface instructions
\newcommand{\Is}{\textbf{ is }}
\newcommand{\To}{\textbf{ to }}
\newcommand{\Downto}{\textbf{ downto }}
\newcommand{\Or}{\textbf{ or }}
\newcommand{\And}{\textbf{ and }}
% Use them inside Math-Mode (Hence the space!)

\begin{document}

\begin{algorithm}

\caption{Searching an element in a sorted array}

\begin{algorithmic}[1]
\Statex
\Procedure{BinarySearch}{$A$,$l$,$r$,$x$}
\If {$l geq r$}
\State $m \gets \lfloor l + (r-1) / 2) \rfloor$
\If {$A[m] == x$}
\State \Return $m$
\EndIf
\If {$A[m] > x$}
\State \Return $BinarySearch(A,l,m-1,x)$
\Else
\State \Return $BinarySearch(A,m+1,r,x)$
\EndIf
\EndIf
\EndProcedure
\end{algorithmic}

\end{algorithm}



\end{document}

0 comments on commit 53592e7

Please sign in to comment.