Skip to content

Commit

Permalink
Merge pull request #18 from dbosk/refactor-vt
Browse files Browse the repository at this point in the history
Refactor based on variation theory
  • Loading branch information
dbosk authored Sep 2, 2024
2 parents da0b9a6 + 40cf37a commit 155306b
Show file tree
Hide file tree
Showing 8 changed files with 347 additions and 242 deletions.
2 changes: 1 addition & 1 deletion didactic
Submodule didactic updated 1 files
+44 −19 didactic.dtx
2 changes: 1 addition & 1 deletion makefiles
Submodule makefiles updated 2 files
+2 −2 tex.mk
+9 −4 tex.mk.nw
1 change: 1 addition & 0 deletions whatis/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ __pycache__/
tests_Makefile
didactic_output_*
didactic_code_*
whatis.tex
134 changes: 84 additions & 50 deletions whatis/bibliography.bib
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ @book{NecessaryConditionsOfLearning
isbn = {978-1317811947},
month = {6},
publisher = {Routledge},
url = {https://doi.org/10.4324/9781315816876},
year = {2014},
year = {2014},
}

@article{Knuth1984,
Expand All @@ -19,8 +18,7 @@ @article{Knuth1984
number = {2},
pages = {97--111},
publisher = {Oxford University Press (OUP)},
url = {https://academic.oup.com/comjnl/article-pdf/27/2/97/981657/270097.pdf},
volume = {27},
volume = {27},
year = {1984},
}

Expand Down Expand Up @@ -51,8 +49,7 @@ @article{Ramsey1994
number = {5},
pages = {97--105},
publisher = {Institute of Electrical and Electronics Engineers (IEEE)},
url = {https://doi.org/10.1109/52.311070},
volume = {11},
volume = {11},
year = {1994},
}

Expand All @@ -75,56 +72,93 @@ @book{Wolfram1988
}

@book{KnuthAOCPvol3,
title = {The Art of Computer Programming: Sorting and Searching},
title = {The Art of Computer Programming: Sorting and Searching},
author = {Knuth, Donald Ervin},
edition = {2},
isbn = {0-201-89685-0},
publisher = {Addison-Wesley},
volume = {3},
edition = {2},
year = {1998},
isbn = {0-201-89685-0},
volume = {3},
year = {1998},
}

@article{Musser1997,
author = {Musser, David R.},
title = {Introspective Sorting and Selection Algorithms},
journal = {Software: Practice and Experience},
volume = {27},
number = {8},
pages = {983-993},
keywords = {quicksort, heapsort, sorting algorithms, introspective
algorithms, hybrid algorithms, generic algorithms, STL},
doi = {https://doi.org/10.1002/(SICI)1097-024X(199708)27:8<983::AID-SPE117>3.0.CO;2-\#},
abstract = {Abstract Quicksort is the preferred in-place sorting algorithm in
many contexts, since its average computing time on uniformly
distributed inputs is Θ(N log N), and it is in fact faster than
most other sorting algorithms on most inputs. Its drawback is
that its worst-case time bound is Θ(N2). Previous attempts to
protect against the worst case by improving the way quicksort
chooses pivot elements for partitioning have increased the
average computing time too much – one might as well use heapsort,
which has a Θ(N log N) worst-case time bound, but is on the
average 2–5 times slower than quicksort. A similar dilemma exists
with selection algorithms (for finding the i-th largest element)
based on partitioning. This paper describes a simple solution to
this dilemma: limit the depth of partitioning, and for
subproblems that exceed the limit switch to another algorithm
with a better worst-case bound. Using heapsort as the ‘stopper’
yields a sorting algorithm that is just as fast as quicksort in
the average case, but also has an Θ(N log N) worst case time
bound. For selection, a hybrid of Hoare's FIND algorithm, which
is linear on average but quadratic in the worst case, and the
Blum–Floyd–Pratt–Rivest–Tarjan algorithm is as fast as Hoare's
algorithm in practice, yet has a linear worst-case time bound.
Also discussed are issues of implementing the new algorithms as
generic algorithms, and accurately measuring their performance in
the framework of the C+:+ Standard Template Library. ©1997 by
John Wiley \& Sons, Ltd.},
year = {1997}
title = {Introspective Sorting and Selection Algorithms},
author = {Musser, David R.},
abstract = {Abstract Quicksort is the preferred in-place sorting algorithm in
many contexts, since its average computing time on uniformly
distributed inputs is Θ(N log N), and it is in fact faster than
most other sorting algorithms on most inputs. Its drawback is
that its worst-case time bound is Θ(N2). Previous attempts to
protect against the worst case by improving the way quicksort
chooses pivot elements for partitioning have increased the
average computing time too much – one might as well use heapsort,
which has a Θ(N log N) worst-case time bound, but is on the
average 2–5 times slower than quicksort. A similar dilemma exists
with selection algorithms (for finding the i-th largest element)
based on partitioning. This paper describes a simple solution to
this dilemma: limit the depth of partitioning, and for
subproblems that exceed the limit switch to another algorithm
with a better worst-case bound. Using heapsort as the ‘stopper’
yields a sorting algorithm that is just as fast as quicksort in
the average case, but also has an Θ(N log N) worst case time
bound. For selection, a hybrid of Hoare's FIND algorithm, which
is linear on average but quadratic in the worst case, and the
Blum–Floyd–Pratt–Rivest–Tarjan algorithm is as fast as Hoare's
algorithm in practice, yet has a linear worst-case time bound.
Also discussed are issues of implementing the new algorithms as
generic algorithms, and accurately measuring their performance in
the framework of the C+:+ Standard Template Library. ©1997 by
John Wiley \& Sons, Ltd.},
doi = {https://doi.org/10.1002/(SICI)1097-024X(199708)27:8<983::AID-SPE117>3.0.CO;2-\#},
issn = {0038-0644},
journal = {Software: Practice and Experience},
keywords = {quicksort, heapsort, sorting algorithms, introspective
algorithms, hybrid algorithms, generic algorithms, STL},
month = {8},
number = {8},
pages = {983-993},
publisher = {Wiley},
volume = {27},
year = {1997},
}

@book{ProgrammingPearls,
title = {Programming Pearls},
author = {Jon Bentley},
publisher = {Addison-Wesley},
year = {1999},
title = {Programming Pearls},
author = {Jon Bentley},
booktitle = {CACM},
doi = {10.1145/32232.315727},
publisher = {Addison-Wesley},
url = {https://dl.acm.org/doi/pdf/10.1145/32232.315727},
year = {1999},
}

@article{ErrorsOfTeX,
title = {The Errors of \TeX},
author = {Knuth, Donald E.},
year = {1989},
issue_date = {July 1989},
publisher = {John Wiley \& Sons, Inc.},
address = {USA},
volume = {19},
number = {7},
issn = {0038-0644},
doi = {10.1002/spe.4380190702},
journal = {Softw. Pract. Exper.},
month = {7},
pages = {607–685},
numpages = {79}
}

@book{CleanCode,
title = {Clean Code: A Handbook of Agile Software Craftsmanship},
author = {Robert C. Martin},
publisher = {Prentice Hall},
year = {2008},
}

@book{CodeComplete,
title={Code complete},
author={McConnell, Steve},
year={2004},
publisher={Pearson Education},
}
18 changes: 9 additions & 9 deletions whatis/introsort.nw
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ We use [[from introsort import *]] to import all functions from the module, so
that we can use them as we would in the same module.
(This makes reading easier.)

\subsubsection{The sorting function: \texttt{sorted}}
\subsection{The sorting function: \texttt{sorted}}

We want to implement the function [[sorted]].
\begin{frame}[fragile]
Expand Down Expand Up @@ -157,7 +157,7 @@ import math
@
\end{frame}

\subsubsection{The sorting algorithm: \texttt{intro\textunderscore sort}}
\subsection{The sorting algorithm: \texttt{intro\textunderscore sort}}

We need a separate function for Intro Sort that we can call recursively and vary
the [[max_depth]] parameter.
Expand Down Expand Up @@ -214,7 +214,7 @@ def test_intro_sort_quick():
@


\subsubsection{Using Quick Sort}
\subsection{Using Quick Sort}

The case using Quick Sort is the simplest case.
We simply calculate the pivot and call [[intro_sort]] recursively on the two
Expand Down Expand Up @@ -533,7 +533,7 @@ def test_partition_one_element():
@


\subsubsection{Controlling sorting behaviour}
\subsection{Controlling sorting behaviour}

We want to be able to control the sorting behaviour.
We noticed above that we added a keyword argument to the [[partition]] function
Expand Down Expand Up @@ -589,7 +589,7 @@ def test_sorted_key_reverse():
\end{frame}


\subsubsection{Using Insertion Sort}
\subsection{Using Insertion Sort}

\begin{frame}[fragile]
<<sort using Insertion Sort>>=
Expand Down Expand Up @@ -702,7 +702,7 @@ def test_insertion_sort_one_element():
@


\subsubsection{Using Heap Sort}
\subsection{Using Heap Sort}

\begin{frame}[fragile]
<<sort using Heap Sort>>=
Expand Down Expand Up @@ -824,7 +824,7 @@ def test_heap_sort():
\end{frame}


\subsubsection{Docstrings and literate programming}
\subsection{Docstrings and literate programming}

We see that despite using literate programming, we used docstrings.
Let's have a look at the docstrings for the functions we've defined.
Expand All @@ -841,15 +841,15 @@ The documentation we've written using literate programming targets someone who
needs to understand our implementation---someone who needs answers to all the
whys, the decisions we made while writing the code.

\subsubsection{The test file}
\subsection{The test file}

The test file [[test_introsort.py]] looks like this after we've tangled it:
\begin{frame}[fragile,allowframebreaks]
\only<presentation>{\inputminted[fontsize=\footnotesize]{python}{test_introsort.py}}
\only<article>{\inputminted{python}{test_introsort.py}}
\end{frame}

\subsubsection{Building the tests}
\subsection{Building the tests}

Normally, the tests resides in a directory [[/tests]], not in [[/src]].
We'll still use that directory structure.
Expand Down
1 change: 1 addition & 0 deletions whatis/notes.tex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

\input{preamble.tex}
\noweboptions{longxref,breakcode}
%\chapterstyle{arthangnum}

\usepackage[noamsthm,notheorems]{beamerarticle}
%\setjobnamebeamerversion{slides}
Expand Down
1 change: 1 addition & 0 deletions whatis/preamble.tex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
\usepackage[utf8]{inputenc}
\usepackage[swedish,british]{babel}
\usepackage{float}
\usepackage[inline]{enumitem}

\usepackage{noweb}
\usepackage{booktabs}
Expand Down
Loading

0 comments on commit 155306b

Please sign in to comment.