<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.algo.informatik.tu-darmstadt.de/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Bencina</id>
	<title>Algowiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.algo.informatik.tu-darmstadt.de/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Bencina"/>
	<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/Special:Contributions/Bencina"/>
	<updated>2026-07-21T12:10:25Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.38.4</generator>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Bellman-Ford&amp;diff=1455</id>
		<title>Bellman-Ford</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Bellman-Ford&amp;diff=1455"/>
		<updated>2014-10-21T17:51:14Z</updated>

		<summary type="html">&lt;p&gt;Bencina: Struktur angepasst&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__NOTOC__&lt;br /&gt;
[[Category:Checkup]]&lt;br /&gt;
[[Category:Algorithm]]&lt;br /&gt;
[[Category:Main Algorithm]]&lt;br /&gt;
==General information==&lt;br /&gt;
'''Algorithmic problem:''' [[All pairs shortest paths]]&lt;br /&gt;
&lt;br /&gt;
'''Prerequisites:'''&lt;br /&gt;
&lt;br /&gt;
'''Type of algorithm:''' loop&lt;br /&gt;
&lt;br /&gt;
'''Auxiliary data:'''&lt;br /&gt;
# A [[Paths|distance-valued]] &amp;lt;math&amp;gt;(n \times n)&amp;lt;/math&amp;gt; matrix &amp;lt;math&amp;gt;M&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;n=|V|&amp;lt;/math&amp;gt;. The eventual contents of &amp;lt;math&amp;gt;M&amp;lt;/math&amp;gt; will be returned as the result of the algorithm.&lt;br /&gt;
# A [[Paths|distance-valued]] &amp;lt;math&amp;gt;(n \times n)&amp;lt;/math&amp;gt; matrix &amp;lt;math&amp;gt;L&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;n=|V|&amp;lt;/math&amp;gt;. This matrix represents the graph and the arc lengths and will not be changed throughout the algorithm. It is &amp;lt;math&amp;gt;L=M^1&amp;lt;/math&amp;gt; in the terminology of Section &amp;quot;Powers of distance matrices&amp;quot; on [[Paths#Powers of distance matrices|this]] page.&lt;br /&gt;
&lt;br /&gt;
==Abstract view==&lt;br /&gt;
'''Invariant:''' After &amp;lt;math&amp;gt;i \ge 0&amp;lt;/math&amp;gt; iterations, &amp;lt;math&amp;gt;M^i (v,w)&amp;lt;/math&amp;gt; contains the length of a shortest &amp;lt;math&amp;gt;(v,w)&amp;lt;/math&amp;gt;-path with at most &amp;lt;math&amp;gt;i+1&amp;lt;/math&amp;gt; arcs (for all &amp;lt;math&amp;gt;v,w \in V&amp;lt;/math&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
'''Variant:''' &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; increases by 1.&lt;br /&gt;
&lt;br /&gt;
'''Break condition:''' &amp;lt;math&amp;gt;i=n-1&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Induction basis==&lt;br /&gt;
'''Abstract view:''' For all &amp;lt;math&amp;gt;v,w \in V&amp;lt;/math&amp;gt;, we set&lt;br /&gt;
# &amp;lt;math&amp;gt;M^0 (v,v):= L(v,v) := 0&amp;lt;/math&amp;gt;;&lt;br /&gt;
# &amp;lt;math&amp;gt;M^0 (v,w):= L(v,w) := \ell (v,w)&amp;lt;/math&amp;gt; if &amp;lt;math&amp;gt;(v,w)\in A&amp;lt;/math&amp;gt;;&lt;br /&gt;
# &amp;lt;math&amp;gt;M^0 (v,w):= L(v,w) := +\infty&amp;lt;/math&amp;gt;, if &amp;lt;math&amp;gt;v\neq w&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;(v,w)\notin A&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Induction step==&lt;br /&gt;
'''Abstract view:''' For all &amp;lt;math&amp;gt;v,w \in V&amp;lt;/math&amp;gt;, we set &amp;lt;math&amp;gt;M^{i+1} (v,w) := \min \{ M^i (v,w) + L(u,w) \mid u \in V \}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
(Note that &amp;lt;math&amp;gt;M^i (v,w) + L(u,w) = M^{i+1} (v,w)&amp;lt;/math&amp;gt; is one of the arguments over which the minimum is taken, so the right-hand side is identical to &amp;lt;math&amp;gt;\min \{ M^i (v,w), \min \{ M^i (v,u)+ L(u,w) \mid u \in V \} \}&amp;lt;/math&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' Obvious.&lt;br /&gt;
&lt;br /&gt;
'''Correctness:''' Follows from the explanations of [[Paths#Powers of distance matrices|powers of the distance matrix]] and from the fact that a shortest path cannot have more than &amp;lt;math&amp;gt;|V|-1&amp;lt;/math&amp;gt; arcs.&lt;br /&gt;
&lt;br /&gt;
==Complexity==&lt;br /&gt;
'''Statement:''' The asymptotic complexity is &amp;lt;math&amp;gt;\Theta (n^4)&amp;lt;/math&amp;gt; in the best and worst case.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' The main loop terminates after &amp;lt;math&amp;gt;n-1&amp;lt;/math&amp;gt; iterations. In each iteration of this loop, we update all &amp;lt;math&amp;gt;n^2&amp;lt;/math&amp;gt; matrix entries, and computing one update value requires &amp;lt;math&amp;gt;\Theta (n)&amp;lt;/math&amp;gt; steps.&lt;/div&gt;</summary>
		<author><name>Bencina</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Dijkstra&amp;diff=1454</id>
		<title>Dijkstra</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Dijkstra&amp;diff=1454"/>
		<updated>2014-10-21T17:48:46Z</updated>

		<summary type="html">&lt;p&gt;Bencina: Tippfehler korrigiert&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Graph Algorithms]]&lt;br /&gt;
[[Category:All Pairs Shortest Paths]]&lt;br /&gt;
&amp;lt;div class=&amp;quot;plainlinks&amp;quot; style=&amp;quot;float:right;margin:0 0 5px 5px; border:1px solid #AAAAAA; width:auto; padding:1em; margin: 0px 0px 1em 1em;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.8em;font-weight:bold;text-align: center;margin:0.2em 0 1em 0&amp;quot;&amp;gt;Dijkstra&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 1em 0; text-align:center&amp;quot;&amp;gt;whatever&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 .5em 0;text-align:center&amp;quot;&amp;gt;[[File:olw_logo1.png|20px]][https://openlearnware.tu-darmstadt.de/#!/resource/dijkstra-2310 Openlearnware]&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Dijstra's algorithm''' is a graph algortihm solving the single-source shortest-paths problem.&lt;br /&gt;
&lt;br /&gt;
== Requirements == &lt;br /&gt;
&lt;br /&gt;
* directed Graph &amp;lt;math&amp;gt;G=(V,E)&amp;lt;/math&amp;gt;&lt;br /&gt;
* weight function &amp;lt;math&amp;gt;w\colon E\to\mathbb R&amp;lt;/math&amp;gt;&lt;br /&gt;
* &amp;lt;math&amp;gt;\forall(u,v)\in E\colon w(u,v)\geq 0&amp;lt;/math&amp;gt;&lt;br /&gt;
* source &amp;lt;math&amp;gt;s\in V&amp;lt;/math&amp;gt;&lt;br /&gt;
* Datastruct &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; &lt;br /&gt;
* Datastruct &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General information ==&lt;br /&gt;
&lt;br /&gt;
'''Algorithmic problem:''' [[Single source shortest paths]]&lt;br /&gt;
&lt;br /&gt;
'''Prerequisities:''' For all &amp;lt;math&amp;gt;\alpha \in A&amp;lt;/math&amp;gt;, it is &amp;lt;math&amp;gt;l(a) \geq 0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Type of algortihm:''' loop&lt;br /&gt;
&lt;br /&gt;
'''Auxiliary data:'''&lt;br /&gt;
# A temporary '''distance value''' &amp;lt;math&amp;gt;\delta(v) \in \R&amp;lt;/math&amp;gt; for each node &amp;lt;math&amp;gt;v \in V&amp;lt;/math&amp;gt;. At termination, it is &amp;lt;math&amp;gt;\delta(v) = \Delta(v)&amp;lt;/math&amp;gt; for all &amp;lt;math&amp;gt;v \in V&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt;\Delta(v)&amp;lt;/math&amp;gt; is the shortest &amp;lt;math&amp;gt;(s,v)&amp;lt;/math&amp;gt;-path.&lt;br /&gt;
# A [[bounded priority queue]] &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt; of size &amp;lt;math&amp;gt;|V|-1&amp;lt;/math&amp;gt;, which contains nodes from &amp;lt;math&amp;gt;V&amp;lt;/math&amp;gt; and takes their &amp;lt;math&amp;gt;\delta&amp;lt;/math&amp;gt;-values as keys. The node with minimal key is returned.&lt;br /&gt;
&lt;br /&gt;
== Abstract view ==&lt;br /&gt;
&lt;br /&gt;
'''Invariant:''' After &amp;lt;math&amp;gt;i \geq 0&amp;lt;/math&amp;gt; iterations:&lt;br /&gt;
# &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt; contains all nodes of &amp;lt;math&amp;gt;V&amp;lt;/math&amp;gt; except &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; further nodes.&lt;br /&gt;
# For the nodes &amp;lt;math&amp;gt;v \in V&amp;lt;/math&amp;gt; not in &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt;, it is &amp;lt;math&amp;gt;\delta(v) = \Delta(v)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# For the nodes &amp;lt;math&amp;gt;v \in V&amp;lt;/math&amp;gt; in &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\delta(v)&amp;lt;/math&amp;gt; is the length of a shortest &amp;lt;math&amp;gt;(s,v)&amp;lt;/math&amp;gt;-path that solely contains nodes not in &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt; (except for &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; itself, of course). As usual, this means &amp;lt;math&amp;gt;\delta(v) = +\infty&amp;lt;/math&amp;gt; if there is no such path.&lt;br /&gt;
&lt;br /&gt;
In particular, it is &amp;lt;math&amp;gt;\delta(v) \geq \Delta(v)&amp;lt;/math&amp;gt; for each node &amp;lt;math&amp;gt;v \in V&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Variant:''' &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; increases by &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Break condition:'''&lt;br /&gt;
* &amp;lt;math&amp;gt;Q = \empty&amp;lt;/math&amp;gt;, which means that all nodes are reachable from &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; and have been processed;&lt;br /&gt;
* otherwise, if &amp;lt;math&amp;gt;\delta(v) = +\infty&amp;lt;/math&amp;gt; for the next node &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; of &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt;, which means that &amp;lt;math&amp;gt;\delta(v) = + \infty&amp;lt;/math&amp;gt; for '''every''' node in &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt; and hence none of the nodes in &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt; is reachable from &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Induction basis ==&lt;br /&gt;
=== Abstract view: ===&lt;br /&gt;
# All nodes of &amp;lt;math&amp;gt;V&amp;lt;/math&amp;gt; except &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; have to be members of &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Root &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; must have correct distance &amp;lt;math&amp;gt;\Delta(s) = 0&amp;lt;/math&amp;gt;.&lt;br /&gt;
# The other nodes must meet Invariant #3.&lt;br /&gt;
&lt;br /&gt;
=== Implementation: ===&lt;br /&gt;
# &amp;lt;math&amp;gt;\delta(s) := 0&amp;lt;/math&amp;gt;;&lt;br /&gt;
# For all &amp;lt;math&amp;gt;a = (s,v) \in A&amp;lt;/math&amp;gt;, set &amp;lt;math&amp;gt;\delta(v) := l(a)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# For all &amp;lt;math&amp;gt;v \in V\setminus\{s\}&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;(s,v) \notin A&amp;lt;/math&amp;gt; set &amp;lt;math&amp;gt;\delta(v) := +\infty&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Insert all nodes in &amp;lt;math&amp;gt;V\setminus\{s\}&amp;lt;/math&amp;gt; into &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Proof: ===&lt;br /&gt;
Obvious.&lt;br /&gt;
&lt;br /&gt;
== Induction step ==&lt;br /&gt;
&lt;br /&gt;
=== Abstract view: ===&lt;br /&gt;
One node is extracted from &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt;, and the distance values of the endnodes of its outgoing arcs are updated to meet Invariant #3.&lt;br /&gt;
&lt;br /&gt;
=== Implementation: ===&lt;br /&gt;
# Extract the next node &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; from &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt;.&lt;br /&gt;
# For each outgoing arc &amp;lt;math&amp;gt;a = (v,w) \in A&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;w \in Q&amp;lt;/math&amp;gt;, set &amp;lt;math&amp;gt;\delta(w) := min\{\delta(w), \delta(v) + l(a)\}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Correctness: ===&lt;br /&gt;
The first invariant is trivially maintained. So we will focus on the second and third invariants.&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the moment immediately before the &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;-iteration. The core of the proof is to show that &amp;lt;math&amp;gt;\delta(v) = \Delta(v)&amp;lt;/math&amp;gt; hold at that moment (and hence forever). For a contradiction, suppose there is an &amp;lt;math&amp;gt;(s,v)&amp;lt;/math&amp;gt;-path &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; whose length is strictly less than &amp;lt;math&amp;gt;\delta(v)&amp;lt;/math&amp;gt;. The third invariant implies that &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; has nodes in &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt; beside &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt;. Let &amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt; denote the first such node on &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt;. Then none of the nodes on the subpath of &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; from &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt; belonged to &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt; at that moment (except for &amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt; itself, of course). Due to the [[Paths|prefix property]], this subpath is a shortest &amp;lt;math&amp;gt;(s,u)&amp;lt;/math&amp;gt;-path, so Invariant #3 implies &amp;lt;math&amp;gt;\delta(u) = \Delta(u)&amp;lt;/math&amp;gt;. On the other hand, the specific choice of &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; implies &amp;lt;math&amp;gt;\delta(u) \geq \delta(v)&amp;lt;/math&amp;gt;. In summary, the subpath of &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; from &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; is not shorter than &amp;lt;math&amp;gt;\delta(v)&amp;lt;/math&amp;gt;. To obtain &amp;lt;math&amp;gt;l(p) &amp;lt; \delta(v)&amp;lt;/math&amp;gt;, the subpath of &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; from &amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; must have negative total length, which contradicts the prerequisite &amp;lt;math&amp;gt;l \geq 0&amp;lt;/math&amp;gt;.&amp;lt;br&amp;gt;&lt;br /&gt;
Now we know &amp;lt;math&amp;gt;\delta(v) = \Delta(v)&amp;lt;/math&amp;gt;. Therefore, &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; fulfills the statement of the second invariant after the &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;-th iteration. Of course, the second invariant is not violated by any other node either, and is thus maintained.&amp;lt;br&amp;gt;&lt;br /&gt;
For a node &amp;lt;math&amp;gt;w \in V&amp;lt;/math&amp;gt; let &amp;lt;math&amp;gt;P_i (w)&amp;lt;/math&amp;gt; denote the set of all &amp;lt;math&amp;gt;(s,w)&amp;lt;/math&amp;gt;-paths such that no node (except for &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; itself) is in &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt; immediately after the &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;-th iteration. In particular, the statement of the third invariant means that &amp;lt;math&amp;gt;\delta(w)&amp;lt;/math&amp;gt; is the length of a shortest path in &amp;lt;math&amp;gt;P_i (w)&amp;lt;/math&amp;gt;. The induction hypothesis says that &amp;lt;math&amp;gt;\delta(w)&amp;lt;/math&amp;gt; is the minimum length of all &amp;lt;math&amp;gt;(s,w)&amp;lt;/math&amp;gt;-paths in &amp;lt;math&amp;gt;P_{i-1} (w)&amp;lt;/math&amp;gt;. The difference &amp;lt;math&amp;gt;P_i (w) \setminus P_{i-1} (w)&amp;lt;/math&amp;gt; consists of all &amp;lt;math&amp;gt;(s,w)&amp;lt;/math&amp;gt;-paths such that &amp;lt;math&amp;gt;(v,w)&amp;lt;/math&amp;gt; is the last arc and all nodes except for &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; belong to &amp;lt;math&amp;gt;V_{i-1}&amp;lt;/math&amp;gt;. Therefore, the second step of the iteration incorporates &amp;lt;math&amp;gt;P_i (w) \setminus P_{i-1} (w)&amp;lt;/math&amp;gt; in the computation of &amp;lt;math&amp;gt;\delta(w)&amp;lt;/math&amp;gt; appropriately to maintain Invaraint #3.&lt;br /&gt;
&lt;br /&gt;
== Complexity ==&lt;br /&gt;
&lt;br /&gt;
'''Statement:''' The worst-case complexity is in &amp;lt;math&amp;gt;O(m+T(n)\cdot n)&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;n = |V|&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;m = |A|&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;T(n)&amp;lt;/math&amp;gt; is the worst-case complexity for extraction/inserting nodes in &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' Each node is inserted in and extracted from &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt; at most once, respectively, which gives &amp;lt;math&amp;gt;O(T(n) \cdot n)&amp;lt;/math&amp;gt; in total. Also, each arc &amp;lt;math&amp;gt;(v,w) \in A&amp;lt;/math&amp;gt; is touched at most once, namely&lt;br /&gt;
* in the initialization if &amp;lt;math&amp;gt;v = s&amp;lt;/math&amp;gt;,&lt;br /&gt;
* when &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; is extracted from &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt; otherwise.&lt;br /&gt;
&lt;br /&gt;
== Pseudocode == &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====DIJKSTRA(''G,w,s'')====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 DIJKSTRA(''G,w,s'')&lt;br /&gt;
 1 INITIALIZE-SINGLE-SOURCE(''G,s'') &lt;br /&gt;
 2      ''S'' = &amp;amp;empty;&lt;br /&gt;
 3      ''Q'' = ''G.V''&lt;br /&gt;
 4      '''while''' ''Q'' &amp;amp;ne; &amp;amp;empty;&lt;br /&gt;
 5            ''u'' = EXTRACT-MIN(''Q'')&lt;br /&gt;
 6            ''S'' = ''S'' &amp;amp;cup; {''u''} &lt;br /&gt;
 7            '''for''' each vertex ''v'' &amp;amp;isin; G.''Adj''[''u'']&lt;br /&gt;
 8                 RELAX(''u,v,w'')&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====RELAX(''u,v,w'')====&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 RELAX(''u,v,w'')&lt;br /&gt;
 1 '''if''' ''v.d'' &amp;gt; ''u.d'' + ''w''(''u,v'')&lt;br /&gt;
 2       ''v.d'' = ''u.d'' + ''w''(''u,v'')&lt;br /&gt;
 3       ''v.&amp;amp;pi;'' = ''u''&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
====INITIALIZE-SINGLE-SOURCE(''G,s'')====&lt;br /&gt;
 INITIALIZE-SINGLE-SOURCE(''G,s'') &lt;br /&gt;
 1 '''for''' each vertex ''v'' &amp;amp;isin; ''G.V''&lt;br /&gt;
 2         ''v.d'' = &amp;amp;infin;&lt;br /&gt;
 3         ''v.&amp;amp;pi;'' = NIL&lt;br /&gt;
 4 ''s.d'' = 0&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further information ==&lt;br /&gt;
# We do not need to insert all nodes except for &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; in &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt;. In fact, it suffices to initially insert the endnodes of the outgoing arcs of &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; and, during an iteration, to insert the nodes of the outgoing arcs of the node that is being extracted from &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt;. In a sense, the nodes of &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt; then constitute the &amp;quot;frontierline&amp;quot; of the algorithm. We choose another variant on Dijkstra's algorithm mainly for simplicity of presentation.&lt;br /&gt;
# Clearly, Dijkstra's algorithm also solves the single-source single-target case. In this case, Dijkstra may safely terminate once has been extracted from &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt;. In fact, the loop invariant implies &amp;lt;math&amp;gt;\delta(t) = \Delta(t)&amp;lt;/math&amp;gt; from then on. This variant on Dijkstra's algorithm is sometimes called the '''early termination variant'''.&lt;br /&gt;
# In the single-source single-target case, two searches may be applied simultaneously: one from &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; on &amp;lt;math&amp;gt;G&amp;lt;/math&amp;gt;, the other one from &amp;lt;math&amp;gt;t&amp;lt;/math&amp;gt; on the [[Basic graph definitions#Transpose of a graph|transpose]] of &amp;lt;math&amp;gt;G&amp;lt;/math&amp;gt;. This variant is called '''bidirectional search'''. It terminates once one node has been seen by both searches. Then either the concatenation of the paths to this node from both searches is a shortest &amp;lt;math&amp;gt;(s,t)&amp;lt;/math&amp;gt;-path, or such a path may be found among the paths of the following kind: There is an arc &amp;lt;math&amp;gt;(v,w)\in A&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; has been seen in the forward search from the source and &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; has been seen in the backward search from &amp;lt;math&amp;gt;t&amp;lt;/math&amp;gt;.&lt;br /&gt;
# In a scenario where we perform a large number of searches on the same graph and arc lengths, we do not need to touch the whole graph in each search: &lt;br /&gt;
## We change the algorithm as suggested by the first point: only keep the &amp;quot;frontierline&amp;quot; in &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt;.&lt;br /&gt;
## We also change the algorithm as suggested by the second point: terminate once &amp;lt;math&amp;gt;t&amp;lt;/math&amp;gt; is processed.&lt;br /&gt;
## To avoid touching all nodes for initializing the &amp;lt;math&amp;gt;\delta&amp;lt;/math&amp;gt;-values, we maintain sort of a '''version counter''', or '''time stamp''', for each node, which holds the ID of the last search that met this node. Whenever a node is met in a search and the nodes version counter does not equal the ID of the current search, the distance value of this node is regarded as &amp;lt;math&amp;gt;+ \infty&amp;lt;/math&amp;gt; (no matter what its current value is).&lt;br /&gt;
# The early termination variant may be modified as follows: for and [[Paths|admissible distance function]] &amp;lt;math&amp;gt;h:V \to \R&amp;lt;/math&amp;gt;, replace length &amp;lt;math&amp;gt;l&amp;lt;/math&amp;gt; by &amp;lt;math&amp;gt;l_h&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt;l_h(a) := l(a) + h(w) - h(v)&amp;lt;/math&amp;gt; for each &amp;lt;math&amp;gt;a = (v,w) \in A&amp;lt;/math&amp;gt;. This variant is usually called '''goal-directed search'''. The heuristic idea is that we (hopefully) reach &amp;lt;math&amp;gt; t&amp;lt;/math&amp;gt; earlier because the arcs that roughly point towards &amp;lt;math&amp;gt;t&amp;lt;/math&amp;gt; are shortened, and the arcs the roughly point in a direction away from &amp;lt;math&amp;gt;t&amp;lt;/math&amp;gt; are lengthened.&lt;/div&gt;</summary>
		<author><name>Bencina</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Bubblesort&amp;diff=1198</id>
		<title>Bubblesort</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Bubblesort&amp;diff=1198"/>
		<updated>2014-10-13T14:29:56Z</updated>

		<summary type="html">&lt;p&gt;Bencina: /* Gerneral information */ Tippfehler korrigiert&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Sorting Algorithms]]&lt;br /&gt;
&amp;lt;div class=&amp;quot;plainlinks&amp;quot; style=&amp;quot;float:right;margin:0 0 5px 5px; border:1px solid #AAAAAA; width:auto; padding:1em; margin: 0px 0px 1em 1em;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.8em;font-weight:bold;text-align: center;margin:0.2em 0 1em 0&amp;quot;&amp;gt;Bubble Sort&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 1em 0; text-align:center&amp;quot;&amp;gt;whatever&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 .5em 0;text-align:center&amp;quot;&amp;gt;[[File:olw_logo1.png|20px]][https://openlearnware.tu-darmstadt.de/#!/resource/bubble-sort-1940 Openlearnware]&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General information ==&lt;br /&gt;
&lt;br /&gt;
'''Algorithmic problem:''' [[Sorting based on pairwise comparison]]&lt;br /&gt;
&lt;br /&gt;
'''Type of algorithm:''' loop&lt;br /&gt;
&lt;br /&gt;
== Abstract view ==&lt;br /&gt;
'''Invariant:''' After &amp;lt;math&amp;gt;i \geq 0&amp;lt;/math&amp;gt; iterations, the elements of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; at the positions &amp;lt;math&amp;gt;|S| - i + 1,\dots,|S|&amp;lt;/math&amp;gt; are placed at their correct positions in sorting order.&lt;br /&gt;
&lt;br /&gt;
'''Variant:''' &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; increases by &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Break condition:''' &amp;lt;math&amp;gt;i = |S| - 1&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Induction basis ==&lt;br /&gt;
&lt;br /&gt;
'''Abstract view:''' Nothing to do.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' Nothing to do.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' Nothing to show.&lt;br /&gt;
&lt;br /&gt;
== Induction step ==&lt;br /&gt;
&lt;br /&gt;
'''Abstract view:''' Step-by-step, move the maximum element seen so far to the position &amp;lt;math&amp;gt;|S| - i + 1&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' For &amp;lt;math&amp;gt;j := 2,\dots,|S|-i+1&amp;lt;/math&amp;gt; (in this order): If &amp;lt;math&amp;gt;S[j-1] &amp;gt; S[j]&amp;lt;/math&amp;gt;, swap &amp;lt;math&amp;gt;S[j-1]&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S[j]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Correctness:''' The loop invariant of the inner loop is this: after &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt; iterations of the inner loop, &amp;lt;math&amp;gt;S[m+1]&amp;lt;/math&amp;gt; is the maximum out of &amp;lt;math&amp;gt;S[1],\dots,S[m+1]&amp;lt;/math&amp;gt; To see that invariant, note that the induction hypothesis implies for an iteration on index &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; that &amp;lt;math&amp;gt;S[j-1]&amp;lt;/math&amp;gt; is the maximum out of &amp;lt;math&amp;gt;S[1],\dots,S[j-1]&amp;lt;/math&amp;gt;. So, if &amp;lt;math&amp;gt;S[j-1] &amp;gt; S[j]&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;S[j-1]&amp;lt;/math&amp;gt; is also the maximum out of &amp;lt;math&amp;gt;S[1],\dots,S[j]&amp;lt;/math&amp;gt;. Therefore, swapping &amp;lt;math&amp;gt;S[j-1]&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S[j]&amp;lt;/math&amp;gt; maintains the invariant. On the other hand, if &amp;lt;math&amp;gt;S[j-1] \leq S[j]&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;S[j]&amp;lt;/math&amp;gt; is already the maximum, so doing nothing maintains the invariant in this case.&lt;br /&gt;
&lt;br /&gt;
== Complexity ==&lt;br /&gt;
&lt;br /&gt;
'''Statement:''' The asymptotic complexity is in &amp;lt;math&amp;gt;\Theta(n^2)&amp;lt;/math&amp;gt; in the best and worst case.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' The asymptotic complexity of the inner loop in the &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;-th iteration of the outer loop is in &amp;lt;math&amp;gt;\Theta(n-i)&amp;lt;/math&amp;gt;. Therefore, the total complexity is in &amp;lt;math&amp;gt;\Theta\left(\sum_{i=1}^{n-1} (n-i)\right) = \Theta\left(\sum_{i=1}^{n-1}i\right) = \Theta\left(\frac{n(n-1)}{2}\right) = \Theta(n^2)&amp;lt;/math&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Bencina</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Dijkstra&amp;diff=1160</id>
		<title>Dijkstra</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Dijkstra&amp;diff=1160"/>
		<updated>2014-10-13T12:09:09Z</updated>

		<summary type="html">&lt;p&gt;Bencina: /* Requirements */ Matheumgebungen eingefügt&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Graph Algorithms]]&lt;br /&gt;
[[Category:All Pairs Shortest Paths]]&lt;br /&gt;
&amp;lt;div class=&amp;quot;plainlinks&amp;quot; style=&amp;quot;float:right;margin:0 0 5px 5px; border:1px solid #AAAAAA; width:auto; padding:1em; margin: 0px 0px 1em 1em;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.8em;font-weight:bold;text-align: center;margin:0.2em 0 1em 0&amp;quot;&amp;gt;Dijkstra&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 1em 0; text-align:center&amp;quot;&amp;gt;whatever&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 .5em 0;text-align:center&amp;quot;&amp;gt;[[File:olw_logo1.png|20px]][https://openlearnware.tu-darmstadt.de/#!/resource/dijkstra-2310 Openlearnware]&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Dijstra's algorithm''' is a graph algortihm solving the single-source shortest-paths problem.&lt;br /&gt;
&lt;br /&gt;
== Requirements == &lt;br /&gt;
&lt;br /&gt;
* directed Graph &amp;lt;math&amp;gt;G=(V,E)&amp;lt;/math&amp;gt;&lt;br /&gt;
* weight function &amp;lt;math&amp;gt;w\colon E\to\mathbb R&amp;lt;/math&amp;gt;&lt;br /&gt;
* &amp;lt;math&amp;gt;\forall(u,v)\in E\colon w(u,v)\geq 0&amp;lt;/math&amp;gt;&lt;br /&gt;
* source &amp;lt;math&amp;gt;s\in V&amp;lt;/math&amp;gt;&lt;br /&gt;
* Datastruct &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; &lt;br /&gt;
* Datastruct &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Pseudocode == &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====DIJKSTRA(''G,w,s'')====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 DIJKSTRA(''G,w,s'')&lt;br /&gt;
 1 INITIALIZE-SINGLE-SOURCE(''G,s'') &lt;br /&gt;
 2      ''S'' = &amp;amp;empty;&lt;br /&gt;
 3      ''Q'' = ''G.V''&lt;br /&gt;
 4      '''while''' ''Q'' &amp;amp;ne; &amp;amp;empty;&lt;br /&gt;
 5            ''u'' = EXTRACT-MIN(''Q'')&lt;br /&gt;
 6            ''S'' = ''S'' &amp;amp;cup; {''u''} &lt;br /&gt;
 7            '''for''' each vertex ''v'' &amp;amp;isin; G.''Adj''[''u'']&lt;br /&gt;
 8                 RELAX(''u,v,w'')&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====RELAX(''u,v,w'')====&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 RELAX(''u,v,w'')&lt;br /&gt;
 1 '''if''' ''v.d'' &amp;gt; ''u.d'' + ''w''(''u,v'')&lt;br /&gt;
 2       ''v.d'' = ''u.d'' + ''w''(''u,v'')&lt;br /&gt;
 3       ''v.&amp;amp;pi;'' = ''u''&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
====INITIALIZE-SINGLE-SOURCE(''G,s'')====&lt;br /&gt;
 INITIALIZE-SINGLE-SOURCE(''G,s'') &lt;br /&gt;
 1 '''for''' each vertex ''v'' &amp;amp;isin; ''G.V''&lt;br /&gt;
 2         ''v.d'' = &amp;amp;infin;&lt;br /&gt;
 3         ''v.&amp;amp;pi;'' = NIL&lt;br /&gt;
 4 ''s.d'' = 0&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Bencina</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Directed_graph&amp;diff=1153</id>
		<title>Directed graph</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Directed_graph&amp;diff=1153"/>
		<updated>2014-10-13T12:02:42Z</updated>

		<summary type="html">&lt;p&gt;Bencina: Seite erstellt&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Definition==&lt;br /&gt;
A directed graph is a graph where every edge has a direction.&lt;/div&gt;</summary>
		<author><name>Bencina</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Bipartite_graph&amp;diff=1144</id>
		<title>Bipartite graph</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Bipartite_graph&amp;diff=1144"/>
		<updated>2014-10-13T11:33:05Z</updated>

		<summary type="html">&lt;p&gt;Bencina: /* Absence of odd cycles */ Kleine formale Korrektur am Beweis vorgenommen.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Checkup]]&lt;br /&gt;
[[Category:Background]]&lt;br /&gt;
[[File:Bipartite-graph.png|200px|thumb|right|A simple bipartite graph]]&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
An undirected graph &amp;lt;math&amp;gt;G=(V,E)&amp;lt;/math&amp;gt; is called '''bipartite''' if there is a partition of &amp;lt;math&amp;gt;V&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;V=V_{1} \dot\cup V_{2}&amp;lt;/math&amp;gt;, such that for every edge &amp;lt;math&amp;gt;\{ v,w \} \in E&amp;lt;/math&amp;gt; it is &amp;lt;math&amp;gt;v \in V_{1} \Leftrightarrow w \in V_{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Absence of odd cycles==&lt;br /&gt;
'''Theorem:''' An undirected graph &amp;lt;math&amp;gt;G=(V,E)&amp;lt;/math&amp;gt; is bipartite if, and only if, &amp;lt;math&amp;gt;G&amp;lt;/math&amp;gt; has no cycle of odd length.&lt;br /&gt;
&lt;br /&gt;
'''Proof:'''&lt;br /&gt;
* First suppose &amp;lt;math&amp;gt;G&amp;lt;/math&amp;gt; contains at least one cycle &amp;lt;math&amp;gt;v_{1} - v_{2} - ... - v_{k} - v_{1}&amp;lt;/math&amp;gt; of odd length &amp;lt;math&amp;gt;k \in \N&amp;lt;/math&amp;gt;. Without loss of generality, we assume &amp;lt;math&amp;gt;v_{1} \in V_{1}&amp;lt;/math&amp;gt;. Then it is &amp;lt;math&amp;gt;v_{i} \in V_{1}&amp;lt;/math&amp;gt; for all odd &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;v_{i} \in V_{2}&amp;lt;/math&amp;gt; for all even &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;. The edge &amp;lt;math&amp;gt;\{ v,w \}&amp;lt;/math&amp;gt; contradicts bipartiteness.&lt;br /&gt;
* Now suppose that &amp;lt;math&amp;gt;G&amp;lt;/math&amp;gt; contains no cycle of odd length. We will prove bipartiteness constructively. Without loss of generality, we may assume that &amp;lt;math&amp;gt;G&amp;lt;/math&amp;gt; is connected (otherwise, each connected component may be considered separately). We choose an node &amp;lt;math&amp;gt;v\in V&amp;lt;/math&amp;gt;, which will be fix, and start with an arbitrary node &amp;lt;math&amp;gt;u \in V&amp;lt;/math&amp;gt; and label each node &amp;quot;&amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;&amp;quot; if its distance (smallest number of edges) from &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; is even, and &amp;quot;&amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt;&amp;quot; otherwise. Suppose for a contradiction that there is an edge &amp;lt;math&amp;gt;\{ v,w \}&amp;lt;/math&amp;gt; such that both endnotes have the same label. Then the shortest paths from &amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; plus this edge from an odd cycle (which is not simple but can be easily reduced to a simple cycle, which is odd again).&lt;/div&gt;</summary>
		<author><name>Bencina</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Bipartite_graph&amp;diff=1141</id>
		<title>Bipartite graph</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Bipartite_graph&amp;diff=1141"/>
		<updated>2014-10-13T11:26:59Z</updated>

		<summary type="html">&lt;p&gt;Bencina: Tippfehler beseitigt&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Checkup]]&lt;br /&gt;
[[Category:Background]]&lt;br /&gt;
[[File:Bipartite-graph.png|200px|thumb|right|A simple bipartite graph]]&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
An undirected graph &amp;lt;math&amp;gt;G=(V,E)&amp;lt;/math&amp;gt; is called '''bipartite''' if there is a partition of &amp;lt;math&amp;gt;V&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;V=V_{1} \dot\cup V_{2}&amp;lt;/math&amp;gt;, such that for every edge &amp;lt;math&amp;gt;\{ v,w \} \in E&amp;lt;/math&amp;gt; it is &amp;lt;math&amp;gt;v \in V_{1} \Leftrightarrow w \in V_{2}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Absence of odd cycles==&lt;br /&gt;
'''Theorem:''' An undirected graph &amp;lt;math&amp;gt;G=(V,E)&amp;lt;/math&amp;gt; is bipartite if, and only if, &amp;lt;math&amp;gt;G&amp;lt;/math&amp;gt; has no cycle of odd length.&lt;br /&gt;
&lt;br /&gt;
'''Proof:'''&lt;br /&gt;
* First suppose &amp;lt;math&amp;gt;G&amp;lt;/math&amp;gt; contains at least one cycle &amp;lt;math&amp;gt;v_{1} - v_{2} - ... - v_{k} - v_{1}&amp;lt;/math&amp;gt; of odd length &amp;lt;math&amp;gt;k \in \N&amp;lt;/math&amp;gt;. Without loss of generality, we assume &amp;lt;math&amp;gt;v_{1} \in V_{1}&amp;lt;/math&amp;gt;. Then it is &amp;lt;math&amp;gt;v_{i} \in V_{1}&amp;lt;/math&amp;gt; for all odd &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;v_{i} \in V_{2}&amp;lt;/math&amp;gt; for all even &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;. The edge &amp;lt;math&amp;gt;\{ v,w \}&amp;lt;/math&amp;gt; contradicts bipartiteness.&lt;br /&gt;
* Now suppose that &amp;lt;math&amp;gt;G&amp;lt;/math&amp;gt; contains no cycle of odd length. We will prove bipartiteness constructively. Without loss of generality, we may assume that &amp;lt;math&amp;gt;G&amp;lt;/math&amp;gt; is connected (otherwise, each connected component may be considered separately). We start with an arbitrary node &amp;lt;math&amp;gt;u \in V&amp;lt;/math&amp;gt; and label each node &amp;quot;&amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;&amp;quot; if its distance (smallest number of edges) from &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; is even, and &amp;quot;&amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt;&amp;quot;, otherwise. Suppose for a contradiction that there is an edge &amp;lt;math&amp;gt;\{ v,w \}&amp;lt;/math&amp;gt; such that both endnotes have the same label. Then the shortest paths from &amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; plus this edge from an odd cycle (which is not simple but can be easily reduced to a simple cycle, which is odd again).&lt;/div&gt;</summary>
		<author><name>Bencina</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=B-tree:_minimum&amp;diff=1139</id>
		<title>B-tree: minimum</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=B-tree:_minimum&amp;diff=1139"/>
		<updated>2014-10-13T11:22:51Z</updated>

		<summary type="html">&lt;p&gt;Bencina: Beschreibung hinzugefügt&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== General Information ==&lt;br /&gt;
&lt;br /&gt;
'''Algorithmic Problem:''' &lt;br /&gt;
&lt;br /&gt;
'''Type of algorithm:''' loop&lt;br /&gt;
&lt;br /&gt;
'''Auxiliary data:''' A pointer p of type &amp;quot;pointer to a B-tree node&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
== Abstract View ==&lt;br /&gt;
'''Invariant:''' Before and after each iteration:&lt;br /&gt;
# '''''p''''' points to some node '''''N''''' of the B-tree.&lt;br /&gt;
&lt;br /&gt;
'''Variant:''' '''''p''''' is redirected from the current node '''''N''''' to the first child of the current node.&lt;br /&gt;
&lt;br /&gt;
'''Break condition:'''&lt;br /&gt;
# '''''p''''' points to a leaf of the B-tree.&lt;br /&gt;
&lt;br /&gt;
==Description of the algorithm==&lt;br /&gt;
To get the minimum of a B-Tree, you have to go to the leftest child while it exists.&lt;br /&gt;
&lt;br /&gt;
== Pseudocode ==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Minimum(''x'')&lt;br /&gt;
 1 '''while''' leaf(''x'') = false&lt;br /&gt;
 2        ''x'' = c_0(''x'')&lt;br /&gt;
 3 '''return''' key_0(''x'')&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Bencina</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=B-tree:_minimum&amp;diff=1137</id>
		<title>B-tree: minimum</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=B-tree:_minimum&amp;diff=1137"/>
		<updated>2014-10-13T11:21:51Z</updated>

		<summary type="html">&lt;p&gt;Bencina: /* Pseudocode */ Notation angepasst&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== General Information ==&lt;br /&gt;
&lt;br /&gt;
'''Algorithmic Problem:''' &lt;br /&gt;
&lt;br /&gt;
'''Type of algorithm:''' loop&lt;br /&gt;
&lt;br /&gt;
'''Auxiliary data:''' A pointer p of type &amp;quot;pointer to a B-tree node&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
== Abstract View ==&lt;br /&gt;
'''Invariant:''' Before and after each iteration:&lt;br /&gt;
# '''''p''''' points to some node '''''N''''' of the B-tree.&lt;br /&gt;
&lt;br /&gt;
'''Variant:''' '''''p''''' is redirected from the current node '''''N''''' to the first child of the current node.&lt;br /&gt;
&lt;br /&gt;
'''Break condition:'''&lt;br /&gt;
# '''''p''''' points to a leaf of the B-tree.&lt;br /&gt;
&lt;br /&gt;
== Pseudocode ==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Minimum(''x'')&lt;br /&gt;
 1 '''while''' leaf(''x'') = false&lt;br /&gt;
 2        ''x'' = c_0(''x'')&lt;br /&gt;
 3 '''return''' key_0(''x'')&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Bencina</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=B-tree:_maximum&amp;diff=1135</id>
		<title>B-tree: maximum</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=B-tree:_maximum&amp;diff=1135"/>
		<updated>2014-10-13T11:21:15Z</updated>

		<summary type="html">&lt;p&gt;Bencina: Seite erstellt&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== General Information ==&lt;br /&gt;
&lt;br /&gt;
'''Algorithmic Problem:''' &lt;br /&gt;
&lt;br /&gt;
'''Type of algorithm:''' loop&lt;br /&gt;
&lt;br /&gt;
'''Auxiliary data:''' A pointer &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; of type &amp;quot;pointer to a B-tree node&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
== Abstract View ==&lt;br /&gt;
'''Invariant:''' Before and after each iteration:&lt;br /&gt;
# &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; points to some node &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; of the B-tree.&lt;br /&gt;
&lt;br /&gt;
'''Variant:''' &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; is redirected from the current node &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; to the first child of the current node.&lt;br /&gt;
&lt;br /&gt;
'''Break condition:'''&lt;br /&gt;
# &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; points to a leaf of the B-tree.&lt;br /&gt;
&lt;br /&gt;
== Description of the algorithm ==&lt;br /&gt;
To get the maximum of a B-Tree, you have to go to the rightest child while it exists.&lt;br /&gt;
&lt;br /&gt;
== Pseudocode ==&lt;br /&gt;
&amp;lt;math&amp;gt;c_n(x)&amp;lt;/math&amp;gt; is the rightest child of a node &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Minimum(''x'')&lt;br /&gt;
 1 '''while''' leaf(''x'') = false&lt;br /&gt;
 2        ''x'' = c_n(''x'')&lt;br /&gt;
 3 '''return''' key_n(''x'')&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Bencina</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Bucketsort&amp;diff=985</id>
		<title>Bucketsort</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Bucketsort&amp;diff=985"/>
		<updated>2014-10-11T19:05:20Z</updated>

		<summary type="html">&lt;p&gt;Bencina: Tippfehler beseitigt&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Sorting Algorithms]]&lt;br /&gt;
&amp;lt;div class=&amp;quot;plainlinks&amp;quot; style=&amp;quot;float:right;margin:0 0 5px 5px; border:1px solid #AAAAAA; width:auto; padding:1em; margin: 0px 0px 1em 1em;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.8em;font-weight:bold;text-align: center;margin:0.2em 0 1em 0&amp;quot;&amp;gt;Bucket Sort&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 1em 0; text-align:center&amp;quot;&amp;gt;whatever&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 .5em 0;text-align:center&amp;quot;&amp;gt;[[File:olw_logo1.png|20px]][https://openlearnware.tu-darmstadt.de/#!/resource/bucket-sort-1941 Openlearnware]&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General information ==&lt;br /&gt;
&lt;br /&gt;
'''Algorithmic problem:''' [[Sorting sequences of strings]]&lt;br /&gt;
&lt;br /&gt;
'''Type of algorithm:''' loop&lt;br /&gt;
&lt;br /&gt;
'''Axiliary data:'''&lt;br /&gt;
# An ordered sequence &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt; of strings, which will eventually hold the overall result of the algorithm.&lt;br /&gt;
# The '''buckets''', that is, an array &amp;lt;math&amp;gt;B&amp;lt;/math&amp;gt; whose index range contains the ID range of &amp;lt;math&amp;gt;\Sigma&amp;lt;/math&amp;gt; (e.g. 26 for the alphabet) and whose components are [[Sets and sequences|ordered sequences]] of strings.&lt;br /&gt;
# Let &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; denote the maximum length of an input string.&lt;br /&gt;
# An array &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; with index range &amp;lt;math&amp;gt;[1,\dots,N]&amp;lt;/math&amp;gt; and [[Sets and sequences|multisets]] of strings as components.&lt;br /&gt;
&lt;br /&gt;
== Abstract view ==&lt;br /&gt;
&lt;br /&gt;
'''Invariant:''' After &amp;lt;math&amp;gt;i \geq 0&amp;lt;/math&amp;gt; iterations:&lt;br /&gt;
# For &amp;lt;math&amp;gt;j \in \{1,\dots,N-i\}&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;A[j]&amp;lt;/math&amp;gt; contains all input strings of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;.&lt;br /&gt;
# &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt; contains all other input strings, that is, the ones with length at least &amp;lt;math&amp;gt;N - i + 1&amp;lt;/math&amp;gt;. The sequence &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt; is sorted according to the following definition of comparison: &amp;lt;math&amp;gt;str1 &amp;lt; str2&amp;lt;/math&amp;gt; (resp. &amp;lt;math&amp;gt;str1 \leq str2&amp;lt;/math&amp;gt;) means that the substring of &amp;lt;math&amp;gt;str1&amp;lt;/math&amp;gt; starting at position &amp;lt;math&amp;gt;N - i + 1&amp;lt;/math&amp;gt; is lexicographically smaller (resp., smaller or equal) than the substring of &amp;lt;math&amp;gt;str2&amp;lt;/math&amp;gt; starting at position &amp;lt;math&amp;gt;N - i + 1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# All buckets are empty.&lt;br /&gt;
&lt;br /&gt;
'''Variant:''' &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; increases by &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Break condition: &amp;lt;math&amp;gt;i = N&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Induction basis ==&lt;br /&gt;
&lt;br /&gt;
'''Abstract view:''' The auxiliary data must be initialized.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:'''&lt;br /&gt;
# &amp;lt;math&amp;gt;S' := \emptyset&amp;lt;/math&amp;gt;&lt;br /&gt;
# For each &amp;lt;math&amp;gt;c \in \Sigma&amp;lt;/math&amp;gt;, set &amp;lt;math&amp;gt;B[c] := \emptyset&amp;lt;/math&amp;gt;&lt;br /&gt;
# For each &amp;lt;math&amp;gt;i \in \{1,\dots,N\}&amp;lt;/math&amp;gt;, set &amp;lt;math&amp;gt;A[i] := \emptyset&amp;lt;/math&amp;gt;&lt;br /&gt;
# Move each string &amp;lt;math&amp;gt;str&amp;lt;/math&amp;gt; in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;A[l(str)]&amp;lt;/math&amp;gt; where &amp;lt;math&amp;gt;l(str)&amp;lt;/math&amp;gt; denotes the length of &amp;lt;math&amp;gt;str&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' Obvious.&lt;br /&gt;
&lt;br /&gt;
== Induction step ==&lt;br /&gt;
'''Abstract view:'''&lt;br /&gt;
# Move each string &amp;lt;math&amp;gt;str&amp;lt;/math&amp;gt; in &amp;lt;math&amp;gt;A[N-i+1]&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;B[str[N-i+1]]&amp;lt;/math&amp;gt;&lt;br /&gt;
# Afterwards: Append each string &amp;lt;math&amp;gt;str&amp;lt;/math&amp;gt; in &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt; at the tail of &amp;lt;math&amp;gt;B[str[N - i + 1]]&amp;lt;/math&amp;gt;. It is essential that the strings are considered in the order in which they are stored in &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt;. (Now &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt; is empty.)&lt;br /&gt;
# For each &amp;lt;math&amp;gt;c \in \Sigma&amp;lt;/math&amp;gt; in ascending order: append &amp;lt;math&amp;gt;B[c]&amp;lt;/math&amp;gt; at the tail of &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt; and make &amp;lt;math&amp;gt;B[c]&amp;lt;/math&amp;gt; empty.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' Obvious&lt;br /&gt;
&lt;br /&gt;
'''Correctness:''' We have to show that the relative order of each pair of strings, &amp;lt;math&amp;gt;str1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;str2&amp;lt;/math&amp;gt;, in the new sequence &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt; are correct. For that, we distinguish between four cases:&lt;br /&gt;
# If &amp;lt;math&amp;gt;str1[N-i+1] \neq str2[N-i+1]&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;str1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;str2&amp;lt;/math&amp;gt; are placed in different buckets in Step 3 of the &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;-th iteration, so they are correctly ordered according to the character at position &amp;lt;math&amp;gt;N - i + 1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# If &amp;lt;math&amp;gt;str1[N-i+1] = str2[N-i+1]&amp;lt;/math&amp;gt; and both strings have length &amp;lt;math&amp;gt;N - i + 1&amp;lt;/math&amp;gt;, their relative order is irrelevant, so nothing is to show.&lt;br /&gt;
# Next, consider the case that &amp;lt;math&amp;gt;str1[N-i+1] = str2[N-i+1]&amp;lt;/math&amp;gt;, one string (say, &amp;lt;math&amp;gt;str1&amp;lt;/math&amp;gt;) has length &amp;lt;math&amp;gt;N - i + 1&amp;lt;/math&amp;gt; and the other one (&amp;lt;math&amp;gt;str2&amp;lt;/math&amp;gt;) has a length strictly greater than &amp;lt;math&amp;gt;N - i + 1&amp;lt;/math&amp;gt;. Since the strings of length &amp;lt;math&amp;gt;N - i + 1&amp;lt;/math&amp;gt; are placed in their buckets prior to the longer strings (cf. Steps 1 and 2), &amp;lt;math&amp;gt;str2&amp;lt;/math&amp;gt; appears after &amp;lt;math&amp;gt;str1&amp;lt;/math&amp;gt;, which is correct.&lt;br /&gt;
# Finally, consider the case that &amp;lt;math&amp;gt;str1[N-i+1] = str2[N-i+1]&amp;lt;/math&amp;gt;, and the lengths of both strings are larger than &amp;lt;math&amp;gt;N - i + 1&amp;lt;/math&amp;gt;. Then the induction hypothesis guarantees the correct relative order due to the specific order in which the strings are considered in Step 2.&lt;br /&gt;
&lt;br /&gt;
== Complexity ==&lt;br /&gt;
'''Statement:''' Let &amp;lt;math&amp;gt;M&amp;lt;/math&amp;gt; denote the total sum of all input string lengths. Then the asymptotic complexity is in &amp;lt;math&amp;gt;\Theta(M)&amp;lt;/math&amp;gt; in the best and the worst case.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' Obviously, the preprocessing takes &amp;lt;math&amp;gt;O(M)&amp;lt;/math&amp;gt; time.&lt;br /&gt;
In the main loop, each character of each string is read exactly once. Obviously, no operation is applied more often than the reading of single characters.&lt;/div&gt;</summary>
		<author><name>Bencina</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Selection_sort&amp;diff=984</id>
		<title>Selection sort</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Selection_sort&amp;diff=984"/>
		<updated>2014-10-11T18:55:08Z</updated>

		<summary type="html">&lt;p&gt;Bencina: Tippfehler beseitigt&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Sorting Algorithms]]&lt;br /&gt;
&amp;lt;div class=&amp;quot;plainlinks&amp;quot; style=&amp;quot;float:right;margin:0 0 5px 5px; border:1px solid #AAAAAA; width:auto; padding:1em; margin: 0px 0px 1em 1em;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.8em;font-weight:bold;text-align: center;margin:0.2em 0 1em 0&amp;quot;&amp;gt;Selection Sort&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 1em 0; text-align:center&amp;quot;&amp;gt;whatever&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 .5em 0;text-align:center&amp;quot;&amp;gt;[[File:olw_logo1.png|20px]][https://openlearnware.tu-darmstadt.de/#!/resource/selection-sort-1948 Openlearnware]&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General information ==&lt;br /&gt;
&lt;br /&gt;
'''Algorithmic problem:''' [[Sorting based on pairwise comparison]]&lt;br /&gt;
&lt;br /&gt;
'''Type of algorithm:''' loop&lt;br /&gt;
&lt;br /&gt;
== Abstract view ==&lt;br /&gt;
&lt;br /&gt;
'''Invariant:''' After &amp;lt;math&amp;gt;i \geq 0&amp;lt;/math&amp;gt; iterations: The elements at positions &amp;lt;math&amp;gt;|S|-i+1,\dots,|S|&amp;lt;/math&amp;gt; are correctly placed in sorting order.&lt;br /&gt;
&lt;br /&gt;
'''Variant:''' &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; increases by &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Break condition:''' &amp;lt;math&amp;gt;i = |S| - 1&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Induction Basis ==&lt;br /&gt;
&lt;br /&gt;
'''Abstract view:''' Nothing to do.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' Nothing to do.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' Nothing to show.&lt;br /&gt;
&lt;br /&gt;
== Induction step ==&lt;br /&gt;
&lt;br /&gt;
'''Abstract view:''' Identify the maximum out of &amp;lt;math&amp;gt;S[1],\dots,S[|S|-i+1]&amp;lt;/math&amp;gt; and then move it, by a swap, to position &amp;lt;math&amp;gt;|S|-i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:'''&lt;br /&gt;
# Set &amp;lt;math&amp;gt;m := 1&amp;lt;/math&amp;gt;&lt;br /&gt;
# For all &amp;lt;math&amp;gt;j=2,\dots,|S|-i+1&amp;lt;/math&amp;gt;: If &amp;lt;math&amp;gt;S[j] &amp;gt; S[m]&amp;lt;/math&amp;gt; set &amp;lt;math&amp;gt;m := j&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Swap &amp;lt;math&amp;gt;S[m]&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S[|S|-i+1]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Correctness:''' Follows immediately from the invariant of the inner loop: &amp;lt;math&amp;gt;S[m]&amp;lt;/math&amp;gt; is the maximum element out of &amp;lt;math&amp;gt;S[1],\dots,S[j]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Complexity ==&lt;br /&gt;
&lt;br /&gt;
'''Statement:''' The asymptotic complexity is in &amp;lt;math&amp;gt;\Theta(n^2)&amp;lt;/math&amp;gt; in the best and worst case.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' The asymptotic complexity of the inner loop in the &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;-th iteration of the outer loop is in &amp;lt;math&amp;gt;\Theta(n - i)&amp;lt;/math&amp;gt;. Therefore, the total complexity is in &amp;lt;math&amp;gt;\Theta\left(\sum_{i=1}^{n-1} (n-i) \right) = \Theta\left(\sum_{i=1}^{n-1} i \right) = \Theta\left(\frac{n(n-1)}{2} \right) = \Theta\left(n^2\right)&amp;lt;/math&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Bencina</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Mergesort&amp;diff=983</id>
		<title>Mergesort</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Mergesort&amp;diff=983"/>
		<updated>2014-10-11T18:51:49Z</updated>

		<summary type="html">&lt;p&gt;Bencina: /* General Information */ Tippfehler beseitigt&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Sorting Algorithms]]&lt;br /&gt;
[[Category:Divide and Conquer]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;plainlinks&amp;quot; style=&amp;quot;float:right;margin:0 0 5px 5px; border:1px solid #AAAAAA; width:auto; padding:1em; margin: 0px 0px 1em 1em;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.8em;font-weight:bold;text-align: center;margin:0.2em 0 1em 0&amp;quot;&amp;gt;Merge Sort&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 1em 0; text-align:center&amp;quot;&amp;gt;whatever&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 .5em 0;text-align:center&amp;quot;&amp;gt;[[File:olw_logo1.png|20px]][https://openlearnware.tu-darmstadt.de/#!/resource/merge-sort-1944 Openlearnware]&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
== General Information ==&lt;br /&gt;
'''Algorithmic problem:''' [[Sorting based on pairwise comparison]]&lt;br /&gt;
&lt;br /&gt;
'''Type of algorithm:''' recursion&lt;br /&gt;
&lt;br /&gt;
== Abstract View ==&lt;br /&gt;
'''Invariant:''' After a recursive call, the input sequence of this recursive call is sorted.&lt;br /&gt;
&lt;br /&gt;
'''Variant:''' For a recursive call on a subsequence &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt; of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, let &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2'&amp;lt;/math&amp;gt; denote the subsequences of &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt; with which Mergesort is called recursively from that call. Then it is &amp;lt;math&amp;gt;|S_1'| \leq \lceil|S'| /2\rceil&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;|S_2'| \leq \lceil|S'| /2\rceil&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Break condition:''' The current subsequence of the recursive call is empty or a singleton.&lt;br /&gt;
&lt;br /&gt;
== Induction Basis ==&lt;br /&gt;
'''Abstract view:''' Nothing to do on an empty sequence or a singleton.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' Ditto.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' An empty set or singleton is trivially sorted.&lt;br /&gt;
&lt;br /&gt;
== Induction Step ==&lt;br /&gt;
'''Abstract view:''' The sequence is divided into two subsequences of approximately half size, it does not matter at all in which way this is done. Both subsequences are sorted recursively using Mergesort. The sorted subsequences are &amp;quot;merged&amp;quot; to one using algorithm [[Merge]].&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' Obvious.&lt;br /&gt;
&lt;br /&gt;
'''Correctness:''' By induction hypothesis, the recursive calls sort correctly. Algorithm [[Merge]] unites two sorted sequences into one sorted sequence.&lt;br /&gt;
&lt;br /&gt;
== Complexity ==&lt;br /&gt;
'''Statement:''' The complexity is in &amp;lt;math&amp;gt;O(n \log n)&amp;lt;/math&amp;gt; in the best and worst case.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' Obviously, the variant is correct. So, the lengths of &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2'&amp;lt;/math&amp;gt; are at most &amp;lt;math&amp;gt;\lceil1/2\rceil&amp;lt;/math&amp;gt; of the length of &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt;. Consequently, the lengths of &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2'&amp;lt;/math&amp;gt; are at least &amp;lt;math&amp;gt;\lfloor1/2\rfloor&amp;lt;/math&amp;gt; of the length of &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt;. In summary, the overall recursion depth is in &amp;lt;math&amp;gt;\Theta(\log n)&amp;lt;/math&amp;gt; in the best and worst case. Next consider the run time of a single recursive call, which receives some &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt; as input and calls Mergesort recursively with two subsequences &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2'&amp;lt;/math&amp;gt;. The run time of this recursive call (disregarding the run times of the recursive calls with &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2'&amp;lt;/math&amp;gt;) is linear in the length of &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt;. Since all recursive calls on the same recursion level operate on pairwise disjoint subsequences, the total run time of all calls on the same recursive level is linear in the length of the original sequence.&lt;/div&gt;</summary>
		<author><name>Bencina</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Bubblesort&amp;diff=982</id>
		<title>Bubblesort</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Bubblesort&amp;diff=982"/>
		<updated>2014-10-11T18:49:05Z</updated>

		<summary type="html">&lt;p&gt;Bencina: /* Complexity */ Tippfehler korrigiert&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Sorting Algorithms]]&lt;br /&gt;
&amp;lt;div class=&amp;quot;plainlinks&amp;quot; style=&amp;quot;float:right;margin:0 0 5px 5px; border:1px solid #AAAAAA; width:auto; padding:1em; margin: 0px 0px 1em 1em;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.8em;font-weight:bold;text-align: center;margin:0.2em 0 1em 0&amp;quot;&amp;gt;Bubble Sort&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 1em 0; text-align:center&amp;quot;&amp;gt;whatever&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 .5em 0;text-align:center&amp;quot;&amp;gt;[[File:olw_logo1.png|20px]][https://openlearnware.tu-darmstadt.de/#!/resource/bubble-sort-1940 Openlearnware]&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Gerneral information ==&lt;br /&gt;
&lt;br /&gt;
'''Algorithmic problem:''' [[Sorting based on pairwise comparison]]&lt;br /&gt;
&lt;br /&gt;
'''Type of algorithm:''' loop&lt;br /&gt;
&lt;br /&gt;
== Abstract view ==&lt;br /&gt;
'''Invariant:''' After &amp;lt;math&amp;gt;i \geq 0&amp;lt;/math&amp;gt; iterations, the elements of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; at the positions &amp;lt;math&amp;gt;|S| - i + 1,\dots,|S|&amp;lt;/math&amp;gt; are placed at their correct positions in sorting order.&lt;br /&gt;
&lt;br /&gt;
'''Variant:''' &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; increases by &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Break condition:''' &amp;lt;math&amp;gt;i = |S| - 1&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Induction basis ==&lt;br /&gt;
&lt;br /&gt;
'''Abstract view:''' Nothing to do.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' Nothing to do.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' Nothing to show.&lt;br /&gt;
&lt;br /&gt;
== Induction step ==&lt;br /&gt;
&lt;br /&gt;
'''Abstract view:''' Step-by-step, move the maximum element seen so far to the position &amp;lt;math&amp;gt;|S| - i + 1&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' For &amp;lt;math&amp;gt;j := 2,\dots,|S|-i+1&amp;lt;/math&amp;gt; (in this order): If &amp;lt;math&amp;gt;S[j-1] &amp;gt; S[j]&amp;lt;/math&amp;gt;, swap &amp;lt;math&amp;gt;S[j-1]&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S[j]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Correctness:''' The loop invariant of the inner loop is this: after &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt; iterations of the inner loop, &amp;lt;math&amp;gt;S[m+1]&amp;lt;/math&amp;gt; is the maximum out of &amp;lt;math&amp;gt;S[1],\dots,S[m+1]&amp;lt;/math&amp;gt; To see that invariant, note that the induction hypothesis implies for an iteration on index &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; that &amp;lt;math&amp;gt;S[j-1]&amp;lt;/math&amp;gt; is the maximum out of &amp;lt;math&amp;gt;S[1],\dots,S[j-1]&amp;lt;/math&amp;gt;. So, if &amp;lt;math&amp;gt;S[j-1] &amp;gt; S[j]&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;S[j-1]&amp;lt;/math&amp;gt; is also the maximum out of &amp;lt;math&amp;gt;S[1],\dots,S[j]&amp;lt;/math&amp;gt;. Therefore, swapping &amp;lt;math&amp;gt;S[j-1]&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S[j]&amp;lt;/math&amp;gt; maintains the invariant. On the other hand, if &amp;lt;math&amp;gt;S[j-1] \leq S[j]&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;S[j]&amp;lt;/math&amp;gt; is already the maximum, so doing nothing maintains the invariant in this case.&lt;br /&gt;
&lt;br /&gt;
== Complexity ==&lt;br /&gt;
&lt;br /&gt;
'''Statement:''' The asymptotic complexity is in &amp;lt;math&amp;gt;\Theta(n^2)&amp;lt;/math&amp;gt; in the best and worst case.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' The asymptotic complexity of the inner loop in the &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;-th iteration of the outer loop is in &amp;lt;math&amp;gt;\Theta(n-i)&amp;lt;/math&amp;gt;. Therefore, the total complexity is in &amp;lt;math&amp;gt;\Theta\left(\sum_{i=1}^{n-1} (n-i)\right) = \Theta\left(\sum_{i=1}^{n-1}i\right) = \Theta\left(\frac{n(n-1)}{2}\right) = \Theta(n^2)&amp;lt;/math&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Bencina</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Quicksort&amp;diff=981</id>
		<title>Quicksort</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Quicksort&amp;diff=981"/>
		<updated>2014-10-11T16:01:24Z</updated>

		<summary type="html">&lt;p&gt;Bencina: /* Proof: */ Weitere Tippfehler beseitigt&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Sorting Algorithms]]&lt;br /&gt;
[[Category:Divide and Conquer]]&lt;br /&gt;
&amp;lt;div class=&amp;quot;plainlinks&amp;quot; style=&amp;quot;float:right;margin:0 0 5px 5px; border:1px solid #AAAAAA; width:auto; padding:1em; margin: 0px 0px 1em 1em;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.8em;font-weight:bold;text-align: center;margin:0.2em 0 1em 0&amp;quot;&amp;gt;Quick Sort&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 1em 0; text-align:center&amp;quot;&amp;gt;whatever&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 .5em 0;text-align:center&amp;quot;&amp;gt;[[File:olw_logo1.png|20px]][https://openlearnware.tu-darmstadt.de/#!/resource/quick-sort-1945 Openlearnware]&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General information ==&lt;br /&gt;
'''Algorithmic problem:''' [[Sorting based on pairwise comparison]]&lt;br /&gt;
&lt;br /&gt;
'''Type of algorithm:''' recursion&lt;br /&gt;
&lt;br /&gt;
== Abstract view ==&lt;br /&gt;
&lt;br /&gt;
'''Invariant:''' After a recursive call, the input sequence of this recursive call is sorted.&lt;br /&gt;
&lt;br /&gt;
'''Variant:''' In each recursive call, the sequence of the callee is strictly shorter than that of the caller.&lt;br /&gt;
&lt;br /&gt;
'''Break condition:''' The sequence is empty or a singleton.&lt;br /&gt;
&lt;br /&gt;
== Induction basis ==&lt;br /&gt;
&lt;br /&gt;
'''Abstract view:''' Nothing to do on an empty sequence or a singleton.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' Ditto.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' Empty sequences and singletons are trivially sorted.&lt;br /&gt;
&lt;br /&gt;
== Induction step ==&lt;br /&gt;
&lt;br /&gt;
=== Abstract view: ===&lt;br /&gt;
# Choose a pivot value &amp;lt;math&amp;gt;p \in [min\{x|x \in S\},\dots,max\{x|x \in S\}]&amp;lt;/math&amp;gt; (note that &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; is not required to be an element of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Partition &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; into sequences, &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt;, such that &amp;lt;math&amp;gt;x &amp;lt; p&amp;lt;/math&amp;gt; for all &amp;lt;math&amp;gt;x \in S_1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;x = p&amp;lt;/math&amp;gt; for all &amp;lt;math&amp;gt;x \in S_2&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;x &amp;gt; p&amp;lt;/math&amp;gt; for all &amp;lt;math&amp;gt;x \in S_3&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Sort &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; recursively.&lt;br /&gt;
# The concatenation of all three lists, &amp;lt;math&amp;gt;S_1 \| S_2 \| S_3&amp;lt;/math&amp;gt;, is the result of the algorithm.&lt;br /&gt;
&lt;br /&gt;
=== Implementation: ===&lt;br /&gt;
&lt;br /&gt;
# Chose &amp;lt;math&amp;gt;p \in [min\{x|x \in S\},\dots,max\{x|x \in S\}]&amp;lt;/math&amp;gt; according to some pivoting rule.&lt;br /&gt;
# &amp;lt;math&amp;gt;S_1 := S_2 := S_3 := \emptyset&amp;lt;/math&amp;gt;.&lt;br /&gt;
# For all &amp;lt;math&amp;gt;x \in S&amp;lt;/math&amp;gt;, append &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; to&lt;br /&gt;
## &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; if &amp;lt;math&amp;gt;x &amp;lt; p&amp;lt;/math&amp;gt;,&lt;br /&gt;
## &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; if &amp;lt;math&amp;gt;x = p&amp;lt;/math&amp;gt;,&lt;br /&gt;
## &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; if &amp;lt;math&amp;gt;x &amp;gt; p&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Call Quicksort on &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; giving &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt;&lt;br /&gt;
# Call Quicksort on &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; giving &amp;lt;math&amp;gt;S_3'&amp;lt;/math&amp;gt;&lt;br /&gt;
# Return &amp;lt;math&amp;gt;S_1' \| S_2' \| S_3'&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Correctness: ===&lt;br /&gt;
&lt;br /&gt;
By induction hypothesis, &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3'&amp;lt;/math&amp;gt; are sorted permutations of &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt;, respectively. In particular &amp;lt;math&amp;gt;S_1' \| S_2 \| S_3'&amp;lt;/math&amp;gt; is a permutation of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. To see that this permutation is sorted, let &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; be two members of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; immediately succeeds &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; in the resulting sequence &amp;lt;math&amp;gt;S_1' \| S_2 \| S_3'&amp;lt;/math&amp;gt;. We have to show &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt;.&lt;br /&gt;
# If &amp;lt;math&amp;gt;x,y \in S_1'&amp;lt;/math&amp;gt; or &amp;lt;math&amp;gt;x,y \in S_3'&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt; resultes from the induction hypothesis.&lt;br /&gt;
# On the other hand, if &amp;lt;math&amp;gt;x,y \in S_2&amp;lt;/math&amp;gt;. It is &amp;lt;math&amp;gt;x = y = p&amp;lt;/math&amp;gt;, which trivially implies &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt;&lt;br /&gt;
# Finally, for following cases, &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt; is implied by the specific way of partitioning &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; into &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3'&amp;lt;/math&amp;gt;:&lt;br /&gt;
## &amp;lt;math&amp;gt;x \in S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y \in S_2&amp;lt;/math&amp;gt;&lt;br /&gt;
## &amp;lt;math&amp;gt;x \in S_2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y \in S_3'&amp;lt;/math&amp;gt;&lt;br /&gt;
## &amp;lt;math&amp;gt;x \in S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y \in S_3'&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Obviously, this case distinction covers all potential cases, so the claim is proved.&lt;br /&gt;
&lt;br /&gt;
== Complexity ==&lt;br /&gt;
&lt;br /&gt;
=== Statement: ===&lt;br /&gt;
In the worst case, the complexity is &amp;lt;math&amp;gt;\Theta(n^2)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If the pivot rule ensures for some &amp;lt;math&amp;gt;\alpha &amp;lt; 1&amp;lt;/math&amp;gt; that the lengths of &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; are at most &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; times the size of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, then it is even &amp;lt;math&amp;gt;O(n \log n)&amp;lt;/math&amp;gt; in the worst case.&lt;br /&gt;
&lt;br /&gt;
If each pivot value is chosen uniformly randomly from members of the respective sequence and if all selections of pivot values are stochastically independent, the average-case complexity is &amp;lt;math&amp;gt;O(n \log n)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Proof: ===&lt;br /&gt;
First note that the complexity for a single recursive call on &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; (disregarding the complexity for the recursive descents on &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; is in &amp;lt;math&amp;gt;O(|S|)&amp;lt;/math&amp;gt;. On each recursive level, all calls are on distinct subsets of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. Therefore, the number of recursive calls with non-empty sequences on one recursive level is in &amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt;. The number of calls with empty sequences on one level is at most twice the total number of calls with non-empty sequences on the previous level. Hence, the number of calls with empty sequences on one recursive level is in &amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt; as well. In summary, the total complexity on a recursive level is &amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt;. So, for the total complexity, it remains to estimate the number of recursive levels.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now consider the first statement. The recursion variant implies that the deepest recursive level is &amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt;. This gives the claimed &amp;lt;math&amp;gt;O(n^2)&amp;lt;/math&amp;gt; in the worst case.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next assume there is a fixed &amp;lt;math&amp;gt;\alpha &amp;lt; 1&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;|S_1| \leq \alpha \cdot|S|&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;|S_3| \leq \alpha \cdot|S|&amp;lt;/math&amp;gt; is guaranteed in each recursive call. Then the length of any sequence on recursive level &amp;lt;math&amp;gt;\#i&amp;lt;/math&amp;gt; is at most &amp;lt;math&amp;gt;\alpha ^ i \cdot |S|&amp;lt;/math&amp;gt;. Therefore, the maximal recursive depth is &amp;lt;math&amp;gt;\lceil \log_{a-1}(n)\rceil&amp;lt;/math&amp;gt;. Since &amp;lt;math&amp;gt;\alpha^{-1} &amp;gt; 1&amp;lt;/math&amp;gt;, the total complexity is in &amp;lt;math&amp;gt;O(n \log n)&amp;lt;/math&amp;gt; in the worst case.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For the last statement, the average-case analysis, first note that the number of comparisons alone has the same asymptotic complexity as the algorithm as a whole. Next note that any &amp;lt;math&amp;gt;x,y \in S&amp;lt;/math&amp;gt; are compared at most once throughout the entire algorithm if, and only if, &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; or &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; is chosen as the pivot value for a subsequence to which both elements belong. For &amp;lt;math&amp;gt;x,y \in S&amp;lt;/math&amp;gt;, let &amp;lt;math&amp;gt;Pr(x,y)&amp;lt;/math&amp;gt; denote the probability that &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; are indeed compared. Since comparison events are distinct and &amp;lt;math&amp;gt;Pr(x,y )\in \{0,1\}&amp;lt;/math&amp;gt; for all &amp;lt;math&amp;gt;x,y \in S&amp;lt;/math&amp;gt;, the [[Expected value|expected number]] of comparisons is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\sum_{x,y \in S, x \neq y} Pr(x,y)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;n := |S|&amp;lt;/math&amp;gt;, and for &amp;lt;math&amp;gt;i,j \in \{1,\dots,n\}&amp;lt;/math&amp;gt;, let &amp;lt;math&amp;gt;Pr(i,j)&amp;lt;/math&amp;gt; denote the probability that the &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;-th and the &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;-th elemet of the eventual sorted sequence are compared throughout the algorithm. Using this notation, we may rewrite the above summation as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\sum_{x,y \in S, x \neq y} Pr(x,y) = \sum_{i=1}^{n-1} \sum_{j = i+1}^{n} Pr(i,j)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For &amp;lt;math&amp;gt;i,j \in \{1,\dots,n\}&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;i &amp;lt; j&amp;lt;/math&amp;gt;, let &amp;lt;math&amp;gt; S_{ij}&amp;lt;/math&amp;gt; denote the subsequence of the eventual sorted sequence that starts with &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; and ends with &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;. The elements &amp;lt;math&amp;gt;\#i&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\#j&amp;lt;/math&amp;gt; are compared if, and only if, &amp;lt;math&amp;gt;\#i&amp;lt;/math&amp;gt; or &amp;lt;math&amp;gt;\#j&amp;lt;/math&amp;gt; is the very first element of &amp;lt;math&amp;gt;S_{ij}&amp;lt;/math&amp;gt; to be chosen as a pivot. The probability of this event is &amp;lt;math&amp;gt;\frac{2}{|S_{ij}|} = \frac{2}{j - i + 1}&amp;lt;/math&amp;gt;, so we obtain&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\sum_{i=1}^{n-1} \sum_{j = i+1}^{n} \frac{2}{j-i+1}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Substituting &amp;lt;math&amp;gt;k := j-i&amp;lt;/math&amp;gt;, this gives&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\sum_{i=1}^{n-1} \sum_{j = i+1}^{n} \frac{2}{j-i+1} = \sum_{i=1}^{n-1} \sum_{k=1}^{n} \frac{2}{k+1} \leq 2(n - 1)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\sum_{k=1}^n \frac{1}{k+1} \leq 2(n-1)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\sum_{k+1}^n \frac{1}{k}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Harmonic_series_(mathematics)#Rate_of_divergence asymptotic behavior] of the [http://en.wikipedia.org/wiki/Harmonic_series_(mathematics) harmonic series] is &amp;lt;math&amp;gt;\Theta(\log n)&amp;lt;/math&amp;gt;, so the last expression is &amp;lt;math&amp;gt;\Theta(n \log n)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Further information ==&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; is an array, &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; cannot be decomposed into subsequences. We would liko to avoid the need for additional arrays and copy operations. Instead, the array should be sorted in-place, that is, by swap operations on pairs of elements. The auxiliary procedure, [[Pivot partitioning by scanning]], is designed exactly for that: it permutes the array such that each of &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; is a subarray. Then each recursive call of Quicksort operates on a subarray of the input array, which is specified by two index pointers.&lt;/div&gt;</summary>
		<author><name>Bencina</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Mergesort&amp;diff=980</id>
		<title>Mergesort</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Mergesort&amp;diff=980"/>
		<updated>2014-10-11T15:50:01Z</updated>

		<summary type="html">&lt;p&gt;Bencina: /* Complexity */ Tippfehler beseitigt&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Sorting Algorithms]]&lt;br /&gt;
[[Category:Divide and Conquer]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;plainlinks&amp;quot; style=&amp;quot;float:right;margin:0 0 5px 5px; border:1px solid #AAAAAA; width:auto; padding:1em; margin: 0px 0px 1em 1em;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.8em;font-weight:bold;text-align: center;margin:0.2em 0 1em 0&amp;quot;&amp;gt;Merge Sort&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 1em 0; text-align:center&amp;quot;&amp;gt;whatever&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 .5em 0;text-align:center&amp;quot;&amp;gt;[[File:olw_logo1.png|20px]][https://openlearnware.tu-darmstadt.de/#!/resource/merge-sort-1944 Openlearnware]&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
== General Information ==&lt;br /&gt;
'''Algorithmic problem:''' Sorting based on pairwise comparison&lt;br /&gt;
&lt;br /&gt;
'''Type of algorithm:''' recursion&lt;br /&gt;
&lt;br /&gt;
== Abstract View ==&lt;br /&gt;
'''Invariant:''' After a recursive call, the input sequence of this recursive call is sorted.&lt;br /&gt;
&lt;br /&gt;
'''Variant:''' For a recursive call on a subsequence &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt; of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, let &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2'&amp;lt;/math&amp;gt; denote the subsequences of &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt; with which Mergesort is called recursively from that call. Then it is &amp;lt;math&amp;gt;|S_1'| \leq \lceil|S'| /2\rceil&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;|S_2'| \leq \lceil|S'| /2\rceil&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Break condition:''' The current subsequence of the recursive call is empty or a singleton.&lt;br /&gt;
&lt;br /&gt;
== Induction Basis ==&lt;br /&gt;
'''Abstract view:''' Nothing to do on an empty sequence or a singleton.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' Ditto.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' An empty set or singleton is trivially sorted.&lt;br /&gt;
&lt;br /&gt;
== Induction Step ==&lt;br /&gt;
'''Abstract view:''' The sequence is divided into two subsequences of approximately half size, it does not matter at all in which way this is done. Both subsequences are sorted recursively using Mergesort. The sorted subsequences are &amp;quot;merged&amp;quot; to one using algorithm [[Merge]].&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' Obvious.&lt;br /&gt;
&lt;br /&gt;
'''Correctness:''' By induction hypothesis, the recursive calls sort correctly. Algorithm [[Merge]] unites two sorted sequences into one sorted sequence.&lt;br /&gt;
&lt;br /&gt;
== Complexity ==&lt;br /&gt;
'''Statement:''' The complexity is in &amp;lt;math&amp;gt;O(n \log n)&amp;lt;/math&amp;gt; in the best and worst case.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' Obviously, the variant is correct. So, the lengths of &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2'&amp;lt;/math&amp;gt; are at most &amp;lt;math&amp;gt;\lceil1/2\rceil&amp;lt;/math&amp;gt; of the length of &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt;. Consequently, the lengths of &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2'&amp;lt;/math&amp;gt; are at least &amp;lt;math&amp;gt;\lfloor1/2\rfloor&amp;lt;/math&amp;gt; of the length of &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt;. In summary, the overall recursion depth is in &amp;lt;math&amp;gt;\Theta(\log n)&amp;lt;/math&amp;gt; in the best and worst case. Next consider the run time of a single recursive call, which receives some &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt; as input and calls Mergesort recursively with two subsequences &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2'&amp;lt;/math&amp;gt;. The run time of this recursive call (disregarding the run times of the recursive calls with &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2'&amp;lt;/math&amp;gt;) is linear in the length of &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt;. Since all recursive calls on the same recursion level operate on pairwise disjoint subsequences, the total run time of all calls on the same recursive level is linear in the length of the original sequence.&lt;/div&gt;</summary>
		<author><name>Bencina</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Mergesort&amp;diff=979</id>
		<title>Mergesort</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Mergesort&amp;diff=979"/>
		<updated>2014-10-11T15:48:21Z</updated>

		<summary type="html">&lt;p&gt;Bencina: /* Induction Step */ Tippfehler beseitigt&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Sorting Algorithms]]&lt;br /&gt;
[[Category:Divide and Conquer]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;plainlinks&amp;quot; style=&amp;quot;float:right;margin:0 0 5px 5px; border:1px solid #AAAAAA; width:auto; padding:1em; margin: 0px 0px 1em 1em;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.8em;font-weight:bold;text-align: center;margin:0.2em 0 1em 0&amp;quot;&amp;gt;Merge Sort&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 1em 0; text-align:center&amp;quot;&amp;gt;whatever&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 .5em 0;text-align:center&amp;quot;&amp;gt;[[File:olw_logo1.png|20px]][https://openlearnware.tu-darmstadt.de/#!/resource/merge-sort-1944 Openlearnware]&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
== General Information ==&lt;br /&gt;
'''Algorithmic problem:''' Sorting based on pairwise comparison&lt;br /&gt;
&lt;br /&gt;
'''Type of algorithm:''' recursion&lt;br /&gt;
&lt;br /&gt;
== Abstract View ==&lt;br /&gt;
'''Invariant:''' After a recursive call, the input sequence of this recursive call is sorted.&lt;br /&gt;
&lt;br /&gt;
'''Variant:''' For a recursive call on a subsequence &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt; of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, let &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2'&amp;lt;/math&amp;gt; denote the subsequences of &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt; with which Mergesort is called recursively from that call. Then it is &amp;lt;math&amp;gt;|S_1'| \leq \lceil|S'| /2\rceil&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;|S_2'| \leq \lceil|S'| /2\rceil&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Break condition:''' The current subsequence of the recursive call is empty or a singleton.&lt;br /&gt;
&lt;br /&gt;
== Induction Basis ==&lt;br /&gt;
'''Abstract view:''' Nothing to do on an empty sequence or a singleton.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' Ditto.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' An empty set or singleton is trivially sorted.&lt;br /&gt;
&lt;br /&gt;
== Induction Step ==&lt;br /&gt;
'''Abstract view:''' The sequence is divided into two subsequences of approximately half size, it does not matter at all in which way this is done. Both subsequences are sorted recursively using Mergesort. The sorted subsequences are &amp;quot;merged&amp;quot; to one using algorithm [[Merge]].&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' Obvious.&lt;br /&gt;
&lt;br /&gt;
'''Correctness:''' By induction hypothesis, the recursive calls sort correctly. Algorithm [[Merge]] unites two sorted sequences into one sorted sequence.&lt;br /&gt;
&lt;br /&gt;
== Complexity ==&lt;br /&gt;
'''Statement:''' The complexity is in &amp;lt;math&amp;gt;O(n \log n)&amp;lt;/math&amp;gt; in the best and worst case.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' Obviously, the variant is correct. So, the lengths of &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2'&amp;lt;/math&amp;gt; are at most &amp;lt;math&amp;gt;\lceil1/2\rceil&amp;lt;/math&amp;gt; of the length of &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt;. Consequently, the lengths of &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2'&amp;lt;/math&amp;gt; are at least &amp;lt;math&amp;gt;\lfloor1/2\rfloor&amp;lt;/math&amp;gt; of the length of &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt;. In summary, the overall recursion depth is in &amp;lt;math&amp;gt;\Theta(\log n)&amp;lt;/math&amp;gt; in the best and worst case. Next consider the run time of a single recursive call, which receives some &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt; as input and calls Mergesort recursively with two sub-sequences &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2'&amp;lt;/math&amp;gt;. The run time of this recursive call (disregarding the run times of the recursive calls with &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2'&amp;lt;/math&amp;gt;) is linear in the length of &amp;lt;math&amp;gt;S'&amp;lt;/math&amp;gt;. Since all recursive calls on the same recursion level operate on pairwise disjoint sub-sequences, the total run time of all calls on the same recursive level is linear in the length of the original sequence.&lt;/div&gt;</summary>
		<author><name>Bencina</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Quicksort&amp;diff=978</id>
		<title>Quicksort</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Quicksort&amp;diff=978"/>
		<updated>2014-10-11T15:47:02Z</updated>

		<summary type="html">&lt;p&gt;Bencina: /* Proof: */ Tippfehler beseitigt&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Sorting Algorithms]]&lt;br /&gt;
[[Category:Divide and Conquer]]&lt;br /&gt;
&amp;lt;div class=&amp;quot;plainlinks&amp;quot; style=&amp;quot;float:right;margin:0 0 5px 5px; border:1px solid #AAAAAA; width:auto; padding:1em; margin: 0px 0px 1em 1em;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.8em;font-weight:bold;text-align: center;margin:0.2em 0 1em 0&amp;quot;&amp;gt;Quick Sort&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 1em 0; text-align:center&amp;quot;&amp;gt;whatever&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 .5em 0;text-align:center&amp;quot;&amp;gt;[[File:olw_logo1.png|20px]][https://openlearnware.tu-darmstadt.de/#!/resource/quick-sort-1945 Openlearnware]&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General information ==&lt;br /&gt;
'''Algorithmic problem:''' [[Sorting based on pairwise comparison]]&lt;br /&gt;
&lt;br /&gt;
'''Type of algorithm:''' recursion&lt;br /&gt;
&lt;br /&gt;
== Abstract view ==&lt;br /&gt;
&lt;br /&gt;
'''Invariant:''' After a recursive call, the input sequence of this recursive call is sorted.&lt;br /&gt;
&lt;br /&gt;
'''Variant:''' In each recursive call, the sequence of the callee is strictly shorter than that of the caller.&lt;br /&gt;
&lt;br /&gt;
'''Break condition:''' The sequence is empty or a singleton.&lt;br /&gt;
&lt;br /&gt;
== Induction basis ==&lt;br /&gt;
&lt;br /&gt;
'''Abstract view:''' Nothing to do on an empty sequence or a singleton.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' Ditto.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' Empty sequences and singletons are trivially sorted.&lt;br /&gt;
&lt;br /&gt;
== Induction step ==&lt;br /&gt;
&lt;br /&gt;
=== Abstract view: ===&lt;br /&gt;
# Choose a pivot value &amp;lt;math&amp;gt;p \in [min\{x|x \in S\},\dots,max\{x|x \in S\}]&amp;lt;/math&amp;gt; (note that &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; is not required to be an element of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Partition &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; into sequences, &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt;, such that &amp;lt;math&amp;gt;x &amp;lt; p&amp;lt;/math&amp;gt; for all &amp;lt;math&amp;gt;x \in S_1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;x = p&amp;lt;/math&amp;gt; for all &amp;lt;math&amp;gt;x \in S_2&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;x &amp;gt; p&amp;lt;/math&amp;gt; for all &amp;lt;math&amp;gt;x \in S_3&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Sort &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; recursively.&lt;br /&gt;
# The concatenation of all three lists, &amp;lt;math&amp;gt;S_1 \| S_2 \| S_3&amp;lt;/math&amp;gt;, is the result of the algorithm.&lt;br /&gt;
&lt;br /&gt;
=== Implementation: ===&lt;br /&gt;
&lt;br /&gt;
# Chose &amp;lt;math&amp;gt;p \in [min\{x|x \in S\},\dots,max\{x|x \in S\}]&amp;lt;/math&amp;gt; according to some pivoting rule.&lt;br /&gt;
# &amp;lt;math&amp;gt;S_1 := S_2 := S_3 := \emptyset&amp;lt;/math&amp;gt;.&lt;br /&gt;
# For all &amp;lt;math&amp;gt;x \in S&amp;lt;/math&amp;gt;, append &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; to&lt;br /&gt;
## &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; if &amp;lt;math&amp;gt;x &amp;lt; p&amp;lt;/math&amp;gt;,&lt;br /&gt;
## &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; if &amp;lt;math&amp;gt;x = p&amp;lt;/math&amp;gt;,&lt;br /&gt;
## &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; if &amp;lt;math&amp;gt;x &amp;gt; p&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Call Quicksort on &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; giving &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt;&lt;br /&gt;
# Call Quicksort on &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; giving &amp;lt;math&amp;gt;S_3'&amp;lt;/math&amp;gt;&lt;br /&gt;
# Return &amp;lt;math&amp;gt;S_1' \| S_2' \| S_3'&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Correctness: ===&lt;br /&gt;
&lt;br /&gt;
By induction hypothesis, &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3'&amp;lt;/math&amp;gt; are sorted permutations of &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt;, respectively. In particular &amp;lt;math&amp;gt;S_1' \| S_2 \| S_3'&amp;lt;/math&amp;gt; is a permutation of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. To see that this permutation is sorted, let &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; be two members of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; immediately succeeds &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; in the resulting sequence &amp;lt;math&amp;gt;S_1' \| S_2 \| S_3'&amp;lt;/math&amp;gt;. We have to show &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt;.&lt;br /&gt;
# If &amp;lt;math&amp;gt;x,y \in S_1'&amp;lt;/math&amp;gt; or &amp;lt;math&amp;gt;x,y \in S_3'&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt; resultes from the induction hypothesis.&lt;br /&gt;
# On the other hand, if &amp;lt;math&amp;gt;x,y \in S_2&amp;lt;/math&amp;gt;. It is &amp;lt;math&amp;gt;x = y = p&amp;lt;/math&amp;gt;, which trivially implies &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt;&lt;br /&gt;
# Finally, for following cases, &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt; is implied by the specific way of partitioning &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; into &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3'&amp;lt;/math&amp;gt;:&lt;br /&gt;
## &amp;lt;math&amp;gt;x \in S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y \in S_2&amp;lt;/math&amp;gt;&lt;br /&gt;
## &amp;lt;math&amp;gt;x \in S_2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y \in S_3'&amp;lt;/math&amp;gt;&lt;br /&gt;
## &amp;lt;math&amp;gt;x \in S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y \in S_3'&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Obviously, this case distinction covers all potential cases, so the claim is proved.&lt;br /&gt;
&lt;br /&gt;
== Complexity ==&lt;br /&gt;
&lt;br /&gt;
=== Statement: ===&lt;br /&gt;
In the worst case, the complexity is &amp;lt;math&amp;gt;\Theta(n^2)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If the pivot rule ensures for some &amp;lt;math&amp;gt;\alpha &amp;lt; 1&amp;lt;/math&amp;gt; that the lengths of &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; are at most &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; times the size of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, then it is even &amp;lt;math&amp;gt;O(n \log n)&amp;lt;/math&amp;gt; in the worst case.&lt;br /&gt;
&lt;br /&gt;
If each pivot value is chosen uniformly randomly from members of the respective sequence and if all selections of pivot values are stochastically independent, the average-case complexity is &amp;lt;math&amp;gt;O(n \log n)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Proof: ===&lt;br /&gt;
First note that the complexity for a single recursive call on &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; (disregarding the complexity for the recursive descents on &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; is in &amp;lt;math&amp;gt;O(|S|)&amp;lt;/math&amp;gt;. On each recursive level, all calls are on distinct subsets of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. Therefore, the number of recursive calls with non-empty sequences on one recursive level is in &amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt;. The number of calls with empty sequences on one level is at most twice the total number of calls with non-empty sequences on the previous level. Hence, the number of calls with empty sequences on one recursive level is in &amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt; as well. In summary, the total complexity on a recursive level is &amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt;. So, for the total complexity, it remains to estimate the number of recursive levels.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now consider the first statement. The recursion variant implies that the deepest recursive level is &amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt;. This gives the claimed &amp;lt;math&amp;gt;O(n^2)&amp;lt;/math&amp;gt; in the worst case.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next assume there is a fixed &amp;lt;math&amp;gt;\alpha &amp;lt; 1&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;|S_1| \leq \alpha \cdot|S|&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;|S_3| \leq \alpha \cdot|S|&amp;lt;/math&amp;gt; is guaranteed in each recursive call. Then the length of any sequence on recursive level &amp;lt;math&amp;gt;\#i&amp;lt;/math&amp;gt; is at most &amp;lt;math&amp;gt;\alpha ^ i \cdot |S|&amp;lt;/math&amp;gt;. Therefore, the maximal recursive depth is &amp;lt;math&amp;gt;\lceil \log_{a-1}(n)\rceil&amp;lt;/math&amp;gt;. Since &amp;lt;math&amp;gt;\alpha^{-1} &amp;gt; 1&amp;lt;/math&amp;gt;, the total complexity is in &amp;lt;math&amp;gt;O(n \log n)&amp;lt;/math&amp;gt; in the worst case.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For the last statement, the average-case analysis, first note that the number of comparisons alone has the same asymptotic complexity as the algorithm as a whole. Next note that any &amp;lt;math&amp;gt;x,y \in S&amp;lt;/math&amp;gt; are compared at most once throughout the entire algorithm if, and only if, &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; or &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; is chosen as the pivot value for a subsequence to which both elements belong. For &amp;lt;math&amp;gt;x,y \in S&amp;lt;/math&amp;gt;, let &amp;lt;math&amp;gt;Pr(x,y)&amp;lt;/math&amp;gt; denote the probability that and are indeed compared. Since comparison events are distinct and &amp;lt;math&amp;gt;Pr(x,y )\in \{0,1\}&amp;lt;/math&amp;gt; for all &amp;lt;math&amp;gt;x,y \in S&amp;lt;/math&amp;gt;, the [[Expected value|expected number]] of comparisons is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\sum_{x,y \in S, x \neq y} Pr(x,y)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;n := |S|&amp;lt;/math&amp;gt;, and for &amp;lt;math&amp;gt;i,j \in \{1,\dots,n\}&amp;lt;/math&amp;gt;, let &amp;lt;math&amp;gt;Pr(i,j)&amp;lt;/math&amp;gt; denote the probability that the &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;-th and the &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;-th elemet of the eventual sorted sequence are compared throughout the algorithm. Using this notation, we may rewrite the above summation as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\sum_{x,y \in S, x \neq y} Pr(x,y) = \sum_{i=1}^{n-1} \sum_{j = i+1}^{n} Pr(i,j)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For &amp;lt;math&amp;gt;i,j \in \{1,\dots,n\}&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;i &amp;lt; j&amp;lt;/math&amp;gt;, let &amp;lt;math&amp;gt; S_{ij}&amp;lt;/math&amp;gt; denote the subsequence of the eventual sorted sequence that starts with &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; and ends with &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;. The elements &amp;lt;math&amp;gt;\#i&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\#j&amp;lt;/math&amp;gt; are compared if, and only if, &amp;lt;math&amp;gt;\#i&amp;lt;/math&amp;gt; or &amp;lt;math&amp;gt;\#j&amp;lt;/math&amp;gt; is the very first element of &amp;lt;math&amp;gt;S_{ij}&amp;lt;/math&amp;gt; to be chosen as a pivot. The probability of this event is &amp;lt;math&amp;gt;\frac{2}{|S_{ij}|} = \frac{2}{j - i + 1}&amp;lt;/math&amp;gt;, so we obtain&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\sum_{i=1}^{n-1} \sum_{j = i+1}^{n} \frac{2}{j-i+1}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Substituting &amp;lt;math&amp;gt;k := j-i&amp;lt;/math&amp;gt;, this gives&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\sum_{i=1}^{n-1} \sum_{j = i+1}^{n} \frac{2}{j-i+1} = \sum_{i=1}^{n-1} \sum_{k=1}^{n} \frac{2}{k+1} \leq 2(n - 1)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\sum_{k=1}^n \frac{1}{k+1} \leq 2(n-1)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\sum_{k+1}^n \frac{1}{k}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Harmonic_series_(mathematics)#Rate_of_divergence asymptotic behavior] of the [http://en.wikipedia.org/wiki/Harmonic_series_(mathematics) harmonic series] is &amp;lt;math&amp;gt;\Theta(\log n)&amp;lt;/math&amp;gt;, so the last expression is &amp;lt;math&amp;gt;\Theta(n \log n)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Further information ==&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; is an array, &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; cannot be decomposed into subsequences. We would liko to avoid the need for additional arrays and copy operations. Instead, the array should be sorted in-place, that is, by swap operations on pairs of elements. The auxiliary procedure, [[Pivot partitioning by scanning]], is designed exactly for that: it permutes the array such that each of &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; is a subarray. Then each recursive call of Quicksort operates on a subarray of the input array, which is specified by two index pointers.&lt;/div&gt;</summary>
		<author><name>Bencina</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Quicksort&amp;diff=977</id>
		<title>Quicksort</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Quicksort&amp;diff=977"/>
		<updated>2014-10-11T15:10:53Z</updated>

		<summary type="html">&lt;p&gt;Bencina: /* Induction step */ Tippfehler korrigiert&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Sorting Algorithms]]&lt;br /&gt;
[[Category:Divide and Conquer]]&lt;br /&gt;
&amp;lt;div class=&amp;quot;plainlinks&amp;quot; style=&amp;quot;float:right;margin:0 0 5px 5px; border:1px solid #AAAAAA; width:auto; padding:1em; margin: 0px 0px 1em 1em;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.8em;font-weight:bold;text-align: center;margin:0.2em 0 1em 0&amp;quot;&amp;gt;Quick Sort&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 1em 0; text-align:center&amp;quot;&amp;gt;whatever&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;font-size: 1.2em; margin:.5em 0 .5em 0;text-align:center&amp;quot;&amp;gt;[[File:olw_logo1.png|20px]][https://openlearnware.tu-darmstadt.de/#!/resource/quick-sort-1945 Openlearnware]&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== General information ==&lt;br /&gt;
'''Algorithmic problem:''' [[Sorting based on pairwise comparison]]&lt;br /&gt;
&lt;br /&gt;
'''Type of algorithm:''' recursion&lt;br /&gt;
&lt;br /&gt;
== Abstract view ==&lt;br /&gt;
&lt;br /&gt;
'''Invariant:''' After a recursive call, the input sequence of this recursive call is sorted.&lt;br /&gt;
&lt;br /&gt;
'''Variant:''' In each recursive call, the sequence of the callee is strictly shorter than that of the caller.&lt;br /&gt;
&lt;br /&gt;
'''Break condition:''' The sequence is empty or a singleton.&lt;br /&gt;
&lt;br /&gt;
== Induction basis ==&lt;br /&gt;
&lt;br /&gt;
'''Abstract view:''' Nothing to do on an empty sequence or a singleton.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' Ditto.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' Empty sequences and singletons are trivially sorted.&lt;br /&gt;
&lt;br /&gt;
== Induction step ==&lt;br /&gt;
&lt;br /&gt;
=== Abstract view: ===&lt;br /&gt;
# Choose a pivot value &amp;lt;math&amp;gt;p \in [min\{x|x \in S\},\dots,max\{x|x \in S\}]&amp;lt;/math&amp;gt; (note that &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; is not required to be an element of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Partition &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; into sequences, &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt;, such that &amp;lt;math&amp;gt;x &amp;lt; p&amp;lt;/math&amp;gt; for all &amp;lt;math&amp;gt;x \in S_1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;x = p&amp;lt;/math&amp;gt; for all &amp;lt;math&amp;gt;x \in S_2&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;x &amp;gt; p&amp;lt;/math&amp;gt; for all &amp;lt;math&amp;gt;x \in S_3&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Sort &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; recursively.&lt;br /&gt;
# The concatenation of all three lists, &amp;lt;math&amp;gt;S_1 \| S_2 \| S_3&amp;lt;/math&amp;gt;, is the result of the algorithm.&lt;br /&gt;
&lt;br /&gt;
=== Implementation: ===&lt;br /&gt;
&lt;br /&gt;
# Chose &amp;lt;math&amp;gt;p \in [min\{x|x \in S\},\dots,max\{x|x \in S\}]&amp;lt;/math&amp;gt; according to some pivoting rule.&lt;br /&gt;
# &amp;lt;math&amp;gt;S_1 := S_2 := S_3 := \emptyset&amp;lt;/math&amp;gt;.&lt;br /&gt;
# For all &amp;lt;math&amp;gt;x \in S&amp;lt;/math&amp;gt;, append &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; to&lt;br /&gt;
## &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; if &amp;lt;math&amp;gt;x &amp;lt; p&amp;lt;/math&amp;gt;,&lt;br /&gt;
## &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; if &amp;lt;math&amp;gt;x = p&amp;lt;/math&amp;gt;,&lt;br /&gt;
## &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; if &amp;lt;math&amp;gt;x &amp;gt; p&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Call Quicksort on &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; giving &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt;&lt;br /&gt;
# Call Quicksort on &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; giving &amp;lt;math&amp;gt;S_3'&amp;lt;/math&amp;gt;&lt;br /&gt;
# Return &amp;lt;math&amp;gt;S_1' \| S_2' \| S_3'&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Correctness: ===&lt;br /&gt;
&lt;br /&gt;
By induction hypothesis, &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3'&amp;lt;/math&amp;gt; are sorted permutations of &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt;, respectively. In particular &amp;lt;math&amp;gt;S_1' \| S_2 \| S_3'&amp;lt;/math&amp;gt; is a permutation of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. To see that this permutation is sorted, let &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; be two members of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; immediately succeeds &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; in the resulting sequence &amp;lt;math&amp;gt;S_1' \| S_2 \| S_3'&amp;lt;/math&amp;gt;. We have to show &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt;.&lt;br /&gt;
# If &amp;lt;math&amp;gt;x,y \in S_1'&amp;lt;/math&amp;gt; or &amp;lt;math&amp;gt;x,y \in S_3'&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt; resultes from the induction hypothesis.&lt;br /&gt;
# On the other hand, if &amp;lt;math&amp;gt;x,y \in S_2&amp;lt;/math&amp;gt;. It is &amp;lt;math&amp;gt;x = y = p&amp;lt;/math&amp;gt;, which trivially implies &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt;&lt;br /&gt;
# Finally, for following cases, &amp;lt;math&amp;gt;x \leq y&amp;lt;/math&amp;gt; is implied by the specific way of partitioning &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; into &amp;lt;math&amp;gt;S_1'&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3'&amp;lt;/math&amp;gt;:&lt;br /&gt;
## &amp;lt;math&amp;gt;x \in S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y \in S_2&amp;lt;/math&amp;gt;&lt;br /&gt;
## &amp;lt;math&amp;gt;x \in S_2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y \in S_3'&amp;lt;/math&amp;gt;&lt;br /&gt;
## &amp;lt;math&amp;gt;x \in S_1'&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;y \in S_3'&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Obviously, this case distinction covers all potential cases, so the claim is proved.&lt;br /&gt;
&lt;br /&gt;
== Complexity ==&lt;br /&gt;
&lt;br /&gt;
=== Statement: ===&lt;br /&gt;
In the worst case, the complexity is &amp;lt;math&amp;gt;\Theta(n^2)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If the pivot rule ensures for some &amp;lt;math&amp;gt;\alpha &amp;lt; 1&amp;lt;/math&amp;gt; that the lengths of &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; are at most &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; times the size of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, then it is even &amp;lt;math&amp;gt;O(n \log n)&amp;lt;/math&amp;gt; in the worst case.&lt;br /&gt;
&lt;br /&gt;
If each pivot value is chosen uniformly randomly from members of the respective sequence and if all selections of pivot values are stochastically independent, the average-case complexity is &amp;lt;math&amp;gt;O(n \log n)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Proof: ===&lt;br /&gt;
First note that the complexity for a single recursive call on &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; (disregarding the complexity for the recursive descents on &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3&amp;gt;) is in &amp;lt;mathO(|S|)&amp;lt;/math&amp;gt;. On each recursive level, all calls are on distinct subsets of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. Therefore, the number of recursive calls with non-empty sequences on one recursive level is in &amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt;. The number of calls with empty sequences on one level is at most twice the total number of calls with non-empty sequences on the previous level. Hence, the number of calls with empty sequences on one recursive level is in &amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt; as well. In summary, the total complexity on a recursive level is &amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt;. So, for the total complexity, it remains to estimate the number of recursive levels.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now consider the first statement. The recursion variant implies that the deepest recursive level is &amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt;. This gives the claimed &amp;lt;math&amp;gt;O(n^2)&amp;lt;/math&amp;gt; in the worst case.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next assume there is a fixed &amp;lt;math&amp;gt;\alpha &amp;lt; 1&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;|S_1| \leq \alpha \cdot|S|&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;|S_3| \leq \alpha \cdot|S|&amp;lt;/math&amp;gt; is guaranteed in each recursive call. Then the length of any sequence on recursive level &amp;lt;math&amp;gt;\#i&amp;lt;/math&amp;gt; is at most &amp;lt;math&amp;gt;\alpha ^ i \cdot |S|&amp;lt;/math&amp;gt;. Therefore, the maximal recursive depth is &amp;lt;math&amp;gt;\lceil \log_{a-1}(n)\rceil&amp;lt;/math&amp;gt;. Since &amp;lt;math&amp;gt;\alpha^{-1} &amp;gt; 1&amp;lt;/math&amp;gt;, the total complexity is in &amp;lt;math&amp;gt;O(n \log n)&amp;lt;/math&amp;gt; in the worst case.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For the last statement, the average-case analysis, first note that the number of comparisons alone has the same asymptotic complexity as the algorithm as a whole. Next note that any &amp;lt;math&amp;gt;x,y \in S&amp;lt;/math&amp;gt; are compared at most once througout the entire algorithm: if, and only if,&amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; or &amp;lt;math&amp;gt;y&amp;lt;/math&amp;gt; is chosen as the pivot value for a subsequence to which both elements belong. For &amp;lt;math&amp;gt;x,y \in S&amp;lt;/math&amp;gt;, let &amp;lt;math&amp;gt;Pr(x,y)&amp;lt;/math&amp;gt; denote the probability that and are indeed compared. Since comparison events are distinct and &amp;lt;math&amp;gt;Pr(x,y \in \{0,1\}&amp;lt;/math&amp;gt; for all &amp;lt;math&amp;gt;x,y \in S&amp;lt;/math&amp;gt;, the [[Expected value|expected number]] of comparisons is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\sum_{x,y \in S, x \neq y} Pr(x,y)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;n := |S|&amp;lt;/math&amp;gt;, and for &amp;lt;math&amp;gt;i,j \in \{1,\dots,n\}&amp;lt;/math&amp;gt;, let &amp;lt;math&amp;gt;Pr(i,j)&amp;lt;/math&amp;gt; denote the probability that the &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;-th and the &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;-th elemet of the eventual, sorted sequence are compared throughtout the algorithm. Using this notation, we may rewrite the above summation as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\sum_{x,y \in S, x \neq y} Pr(x,y) = \sum_{i=1}^{n-1} \sum_{j = i+1}^{n} Pr(i,j)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For &amp;lt;math&amp;gt;i,j \in \{1,\dots,n\}&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;i &amp;lt; j&amp;lt;/math&amp;gt;, let &amp;lt;math&amp;gt; S_{ij}&amp;lt;/math&amp;gt; denote the subsequence of the eventual, sorted sequence that starts with &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; and ends with &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;. The elements &amp;lt;math&amp;gt;\#i&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\#j&amp;lt;/math&amp;gt; are compared if, and only if, &amp;lt;math&amp;gt;\#i&amp;lt;/math&amp;gt; or &amp;lt;math&amp;gt;\#j&amp;lt;/math&amp;gt; is the very first element of &amp;lt;math&amp;gt;S_{ij}&amp;lt;/math&amp;gt; to be chosen as a pivot. The probability of this event is &amp;lt;math&amp;gt;\frac{2}{|S_{ij}|} = \frac{2}{j - i + 1}&amp;lt;/math&amp;gt;, so we obtain&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\sum_{i=1}^{n-1} \sum_{j = i+1}^{n} \frac{2}{j-i+1}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Substituting &amp;lt;math&amp;gt;k := j-i&amp;lt;/math&amp;gt;, this gives&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\sum_{i=1}^{n-1} \sum_{j = i+1}^{n} \frac{2}{j-i+1} = \sum_{i=1}^{n-1} \sum_{k=1}^{n} \frac{2}{k+1} \leq 2(n - 1)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\sum_{k=1}^n \frac{1}{k+1} \leq 2(n-1)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\sum_{k+1}^n \frac{1}{k}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Harmonic_series_(mathematics)#Rate_of_divergence asymptotic behavior] of the [http://en.wikipedia.org/wiki/Harmonic_series_(mathematics) harmonic series] is &amp;lt;math&amp;gt;\Theta(\log n)&amp;lt;/math&amp;gt;, so the last expression is &amp;lt;math&amp;gt;\Theta(n \log n)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Further information ==&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; is an array, &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; cannot be decomposed into subsequences. We would liko to avoid the need for additional arrays and copy operations. Instead, the array should be sorted in-place, that is, by swap operations on pairs of elements. The auxiliary procedure, [[Pivot partitioning by scanning]], is designed exactly for that: it permutes the array such that each of &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; is a subarray. Then each recursive call of Quicksort operates on a subarray of the input array, which is specified by two index pointers.&lt;/div&gt;</summary>
		<author><name>Bencina</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Bubble&amp;diff=976</id>
		<title>Bubble</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Bubble&amp;diff=976"/>
		<updated>2014-10-11T15:01:48Z</updated>

		<summary type="html">&lt;p&gt;Bencina: /* Complexity */ Schönheitskorrekturen (keine inhaltliche Änderung)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Algorithm]]&lt;br /&gt;
[[Category:Auxiliary Algorithm]]&lt;br /&gt;
&lt;br /&gt;
== General Information ==&lt;br /&gt;
&lt;br /&gt;
'''Algorithmic problem:''' Moving the maximum element&lt;br /&gt;
&lt;br /&gt;
'''Type of algorithm:''' loop&lt;br /&gt;
&lt;br /&gt;
== Abstract View ==&lt;br /&gt;
&lt;br /&gt;
'''Invariant:''' After &amp;lt;math&amp;gt;i \geq 0&amp;lt;/math&amp;gt; iterations, the element at position &amp;lt;math&amp;gt;l + i&amp;lt;/math&amp;gt; is the maximum of the elements at the position &amp;lt;math&amp;gt;l,\dots,l+i&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Variant:''' &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; increases by &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Break condition:''' &amp;lt;math&amp;gt;i = r - l&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Induction Basis ==&lt;br /&gt;
&lt;br /&gt;
'''Abstract view:''' Nothing to do.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' Nothing to do.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' Nothing to show.&lt;br /&gt;
&lt;br /&gt;
== Induction Step ==&lt;br /&gt;
&lt;br /&gt;
'''Abstract view:''' Exchange the elements at positions &amp;lt;math&amp;gt;l + i -1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;l + i&amp;lt;/math&amp;gt; if necessary.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' If &amp;lt;math&amp;gt;S[l+i-1] &amp;gt; S[l+i]&amp;lt;/math&amp;gt;, swap &amp;lt;math&amp;gt;S[l+i-1]&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S[l+i]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Correctness:''' The induction hypothesis implies that, immediately before the &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;-th iteration, &amp;lt;math&amp;gt;S[l+i-1]&amp;lt;/math&amp;gt; is the maximum of the elements at the positions &amp;lt;math&amp;gt;l,\dots,l+i-1&amp;lt;/math&amp;gt;. If &amp;lt;math&amp;gt;S[l+i-1] &amp;gt; S[l+i]&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;S[l+i-1]&amp;lt;/math&amp;gt; is also the maximum out of &amp;lt;math&amp;gt;1,\dots,l+i&amp;lt;/math&amp;gt; and should thus be moved to the position &amp;lt;math&amp;gt;l+i&amp;lt;/math&amp;gt;. Otherwise &amp;lt;math&amp;gt;S[l+i]&amp;lt;/math&amp;gt; is at least as large as all elements at positions &amp;lt;math&amp;gt;l+1,\dots,l+i-1&amp;lt;/math&amp;gt; and should thus stay at position &amp;lt;math&amp;gt;l+i&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Complexity ==&lt;br /&gt;
'''Statement:''' The asymptotic complexity is &amp;lt;math&amp;gt;\Theta(r-l)&amp;lt;/math&amp;gt; in the best and worst case.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' Obvious.&lt;/div&gt;</summary>
		<author><name>Bencina</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Bubble&amp;diff=975</id>
		<title>Bubble</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Bubble&amp;diff=975"/>
		<updated>2014-10-11T14:57:43Z</updated>

		<summary type="html">&lt;p&gt;Bencina: /* Induction Step */ Tippfehler beseitigt&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Algorithm]]&lt;br /&gt;
[[Category:Auxiliary Algorithm]]&lt;br /&gt;
&lt;br /&gt;
== General Information ==&lt;br /&gt;
&lt;br /&gt;
'''Algorithmic problem:''' Moving the maximum element&lt;br /&gt;
&lt;br /&gt;
'''Type of algorithm:''' loop&lt;br /&gt;
&lt;br /&gt;
== Abstract View ==&lt;br /&gt;
&lt;br /&gt;
'''Invariant:''' After &amp;lt;math&amp;gt;i \geq 0&amp;lt;/math&amp;gt; iterations, the element at position &amp;lt;math&amp;gt;l + i&amp;lt;/math&amp;gt; is the maximum of the elements at the position &amp;lt;math&amp;gt;l,\dots,l+i&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Variant:''' &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; increases by &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Break condition:''' &amp;lt;math&amp;gt;i = r - l&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Induction Basis ==&lt;br /&gt;
&lt;br /&gt;
'''Abstract view:''' Nothing to do.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' Nothing to do.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' Nothing to show.&lt;br /&gt;
&lt;br /&gt;
== Induction Step ==&lt;br /&gt;
&lt;br /&gt;
'''Abstract view:''' Exchange the elements at positions &amp;lt;math&amp;gt;l + i -1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;l + i&amp;lt;/math&amp;gt; if necessary.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' If &amp;lt;math&amp;gt;S[l+i-1] &amp;gt; S[l+i]&amp;lt;/math&amp;gt;, swap &amp;lt;math&amp;gt;S[l+i-1]&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S[l+i]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Correctness:''' The induction hypothesis implies that, immediately before the &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;-th iteration, &amp;lt;math&amp;gt;S[l+i-1]&amp;lt;/math&amp;gt; is the maximum of the elements at the positions &amp;lt;math&amp;gt;l,\dots,l+i-1&amp;lt;/math&amp;gt;. If &amp;lt;math&amp;gt;S[l+i-1] &amp;gt; S[l+i]&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;S[l+i-1]&amp;lt;/math&amp;gt; is also the maximum out of &amp;lt;math&amp;gt;1,\dots,l+i&amp;lt;/math&amp;gt; and should thus be moved to the position &amp;lt;math&amp;gt;l+i&amp;lt;/math&amp;gt;. Otherwise &amp;lt;math&amp;gt;S[l+i]&amp;lt;/math&amp;gt; is at least as large as all elements at positions &amp;lt;math&amp;gt;l+1,\dots,l+i-1&amp;lt;/math&amp;gt; and should thus stay at position &amp;lt;math&amp;gt;l+i&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Complexity ==&lt;br /&gt;
'''&lt;br /&gt;
Statement:''' The asymptotic complexity is &amp;lt;math&amp;gt;\Theta(r-l)&amp;lt;/math&amp;gt; in the best and worst case.&lt;br /&gt;
'''&lt;br /&gt;
Proof:''' Obvious.&lt;/div&gt;</summary>
		<author><name>Bencina</name></author>
	</entry>
</feed>