Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding heapsort #142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Pseudocode/Heapsort/code-and-explanation.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
% 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}

% Remove the 'end' at the end of the algorithm
\usepackage[noend]{algpseudocode}

% 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}

\begin{document}
\begin{algorithm}
\caption{Heapsort}
\begin{algorithmic}
\Function{heapSort}{A}
\State buildHeap(A)
\For{i $\leq$ length(A) downto 2}
\State exchange A[1], A[i]
\State heapsize--
\State heapify(A, 1)
\EndFor
\EndFunction
\Function{buildHeap}{A}
\State heapsize $\le$ length(A)
\For{i $\le$ floor($\frac{length}{2}$) downto 1}
\State heapifiy(A, i)
\EndFor
\EndFunction
\Function{heapify}{A, i}
\State le $\gets$ left(i)
\State re $\gets$ right(i)
\If{le $\leq$ heapsize \&\& (A[le]$>$A[re])}
\State largest $\gets$ le
\Else
\State largest $\gets$ i
\EndIf
\If{re $\leq$ heapsize \&\& (A[re] $>$A[largest])}
\State largest $\gets$ re
\EndIf
\If{largest $\neq$ i}
\State change A[i], A[largest]
\State heapify(A, largest)
\EndIf
\EndFunction
\end{algorithmic}
\end{algorithm}
\textbf{Note} : When using heapsort, what you want to do is to build a heap over an unsorted array. After heapyfieing that array, you'll take the root of the heap and stack into the sorted part of the array. Proceed until no elements are left to sort in the array. \\ \\
Keep in mind : the heapsort is an unstable algorithm. It is part of the $\mathcal{O}(n * log n)$ class. For more information refer to the wiki page: https://de.wikipedia.org/wiki/Heapsort
\end{document}