Skip to content
Snippets Groups Projects
Commit 367a82c0 authored by Florian Unger's avatar Florian Unger
Browse files

added appendix with code

parent 7ad0f48f
No related branches found
No related tags found
No related merge requests found
\chapter{Programmcode}
\lstset{
language=Python,
tabsize=2,
rulesepcolor=\color{gray},
xleftmargin=00pt,
framexleftmargin=00pt,
keywordstyle=\color{blue}\bf,
commentstyle=\color{gray},
stringstyle=\color{red},
numbers=left,
numberstyle=\tiny,
numbersep=5pt,
breaklines=true,
showstringspaces=false,
basicstyle=\fontsize{7}{9}\selectfont\ttfamily,
emph={str},emphstyle={\color{magenta}}}
\section{\texttt{quicksort} in Python}
\lstinputlisting[language=Python]{code/quicksort.py}
\documentclass[ngerman, a4paper, oneside]{scrbook} \documentclass[ngerman, a4paper, oneside, appendixprefix]{book}
\usepackage[ngerman]{babel} \usepackage[ngerman]{babel}
%encoding %encoding
\usepackage[utf8]{inputenc} \usepackage[utf8]{inputenc}
\usepackage{alphabeta} \usepackage{alphabeta}
%appendix (copied from SE)
%\usepackage{xpatch}
%\xapptocmd{\appendix}{%
% \addtocontents{toc}{%
% \RedeclareSectionCommand[
% tocdynnumwidth,
% tocentrynumberformat=\tocappendixnumber
% ]{chapter}%
% }%
%}{}{\PatchFailed}
%\newcommand\tocappendixnumber[1]{\chapapp~#1}
%math symbols %math symbols
\usepackage{amsmath} \usepackage{amsmath}
...@@ -29,6 +40,9 @@ ...@@ -29,6 +40,9 @@
\SetEndCharOfAlgoLine{} \SetEndCharOfAlgoLine{}
\usepackage{textcomp} \usepackage{textcomp}
%code listings
\usepackage{listings}
%figures %figures
\usepackage{subcaption} \usepackage{subcaption}
%internal figures %internal figures
...@@ -45,6 +59,7 @@ ...@@ -45,6 +59,7 @@
%other %other
\usepackage{scrextend} \usepackage{scrextend}
%\usepackage{hhline} %\usepackage{hhline}
\usepackage{appendix}
%custom macros %custom macros
\makeatletter \makeatletter
...@@ -62,4 +77,7 @@ ...@@ -62,4 +77,7 @@
\input{100_Grundlagen} \input{100_Grundlagen}
\input{200_Sortieralgorithmen} \input{200_Sortieralgorithmen}
\input{300_Datenstrukturen} \input{300_Datenstrukturen}
\appendix
\input{901_Appendix_Code}
\end{document} \end{document}
def swap(A,i,j):
A[i], A[j] = A[j], A[i]
return A
def partition(A):
p = A[-1]
print("A is {} as partition is called, pivot is {}".format(A, p))
i = -1
for j in range(len(A)-1):
if A[j] <= p:
A = swap(A,i+1,j)
i += 1
print("A is {} after swapping i={} and j={} during partition as {} <= {}".format(A,i,j,A[i],p))
A = swap(A,i+1,-1)
i += 1
print("A is {} after swapping {} and {} in final swap in partition, returning A and pivot index {} to quicksort".format(A,i,j,i))
return (A,i)
def quicksort(A):
print("A is {} as quicksort is called".format(A))
if len(A) > 1:
A,k = partition(A)
print("A is {} after partition, calculating now: quicksort({}) + [A[{}]] + quicksort({}) ".format(A, A[:k], k, A[k+1:]))
return quicksort(A[:k]) + [A[k]] + quicksort(A[k+1:])
else:
return A
A = [7,2,1,8,6,3,5,4] # see https://www.youtube.com/watch?v=MZaf_9IZCrc for step-for-step explanation of the first partition here
# A = [34,56,99,46,23,16,23,11,23,56] # a more complex case, might be helpful ...
A = quicksort(A)
print("A was sorted to {}".format(A))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment