summaryrefslogtreecommitdiff
path: root/documentation/bubblesort.html
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-03-07 13:21:55 +0100
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-03-07 13:21:55 +0100
commit17c723479fc2aa7de69285771c7d91eb51bef288 (patch)
tree10f6475e5907e49b770e5ba9226d6ac05444487d /documentation/bubblesort.html
parent778c45ac7507a995d0fa3f1892477172e7fc020d (diff)
added initial bubblesort example
Diffstat (limited to 'documentation/bubblesort.html')
-rw-r--r--documentation/bubblesort.html47
1 files changed, 47 insertions, 0 deletions
diff --git a/documentation/bubblesort.html b/documentation/bubblesort.html
new file mode 100644
index 0000000..8d66a84
--- /dev/null
+++ b/documentation/bubblesort.html
@@ -0,0 +1,47 @@
+<h1>How fast is a C++ extension</h1>
+<p>
+ Native extensions are fast. But how fast are they? We can demonstrate this
+ with a very simple extension: bubblesort.
+</p>
+<p>
+ Bubblesort is a very inefficient sorting algorithm that is never used in
+ real world software - but that is often used in universities to demonstrate
+ algorithms. We will show you an implementation of this algorithm in PHP
+ and in C++ - and see how much faster the C++ code is.
+</p>
+<p>
+<pre class="language-php">
+&lt;?php
+
+/**
+ * Bubblesort function in parameter
+ *
+ * This function takes an unsorted array as input, sorts it, and returns
+ * the output. It only uses normal PHP operation, and does not rely on
+ * any builting PHP functions or on functions from extensions
+ *
+ * @param array An unsorted array of integers
+ * @return array A sorted array
+ */
+function bubblesort(array $input)
+{
+ // number of elements in the array
+ $count = count($input);
+
+ // loop through the array
+ for ($i = 0; $i &lt; $count; $i++)
+ {
+ // loop through the elements that were already processed
+ for ($j = 0; $j &lt; $count - $i; $j++)
+ {
+ // move on if smaller
+ if ($input[$j] &lt;= $input[$j+1]) continue;
+
+ // swap elements
+ $temp = $input[$j];
+ $input[$j] = $input[$j+1];
+ $input[$j+1] = $temp;
+ }
+ }
+}
+?&gt;