summaryrefslogtreecommitdiff
path: root/documentation/bubblesort.html
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-03-07 13:34:07 +0100
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-03-07 13:34:07 +0100
commit94ecf3170dc0cf758a738f0a3a0ffcc7f01f624b (patch)
tree29417f517b870538746c162c4b72e6178470bcbf /documentation/bubblesort.html
parent17c723479fc2aa7de69285771c7d91eb51bef288 (diff)
improved bubblesort example
Diffstat (limited to 'documentation/bubblesort.html')
-rw-r--r--documentation/bubblesort.html86
1 files changed, 79 insertions, 7 deletions
diff --git a/documentation/bubblesort.html b/documentation/bubblesort.html
index 8d66a84..126ab21 100644
--- a/documentation/bubblesort.html
+++ b/documentation/bubblesort.html
@@ -10,11 +10,11 @@
and in C++ - and see how much faster the C++ code is.
</p>
<p>
-<pre class="language-php">
+<pre class="language-php"><code>
&lt;?php
/**
- * Bubblesort function in parameter
+ * Bubblesort function in PHP
*
* 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
@@ -23,7 +23,7 @@
* @param array An unsorted array of integers
* @return array A sorted array
*/
-function bubblesort(array $input)
+function scripted_bubblesort(array $input)
{
// number of elements in the array
$count = count($input);
@@ -32,16 +32,88 @@ function bubblesort(array $input)
for ($i = 0; $i &lt; $count; $i++)
{
// loop through the elements that were already processed
- for ($j = 0; $j &lt; $count - $i; $j++)
+ for ($j = 1; $j &lt; $count - $i; $j++)
{
// move on if smaller
- if ($input[$j] &lt;= $input[$j+1]) continue;
+ if ($input[$j-1] &lt;= $input[$j]) continue;
// swap elements
$temp = $input[$j];
- $input[$j] = $input[$j+1];
- $input[$j+1] = $temp;
+ $input[$j] = $input[$j-1];
+ $input[$j-1] = $temp;
}
}
}
?&gt;
+</code></pre>
+</p>
+<p>
+ And now exactly the same algorithm, but not as a PHP script, but a native
+ C++ extension.
+</p>
+<p>
+<pre class="language-c++"><code>
+#include &lt;phpcpp.h&gt;
+
+/**
+ * Bubblesort function in C++
+ *
+ * This function takes an unsorted array as input, sorts it, and returns
+ * the output. Notice that we have not really done our best to make the
+ * implementation of this function as efficient as possible - we use stl
+ * containers for example - it is simple looking plain C++ function with
+ * a lot of room for improvements.
+ *
+ * @param array An unsorted array of integers
+ * @return array A sorted array
+ */
+Php::Value native_bubblesort(Php::Parameters &params)
+{
+ // there is one input array, cast the PHP variable to a vector of ints
+ std::vector<int> input = params[0];
+
+ // loop through the array
+ for (size_t i = 0; i &lt; input.size(); i++)
+ {
+ // loop through the elements that were already processed
+ for (size_t j = 1; j &lt; input.size() - i; j++)
+ {
+ // move on if smaller
+ if (input[j-1] &lt;= input[j]) continue;
+
+ // swap elements
+ temp = input[j];
+ input[j] = input[j-1];
+ input[j-1] = temp;
+ }
+ }
+}
+
+/**
+ * Switch to C context, because the Zend-engine calls the get_module() method
+ * in C context, and not in C++ context
+ */
+extern "C" {
+
+ /**
+ * When a PHP extension starts up, the Zend engine calls the get_module()
+ * function to find out which functions and classes are offered by the
+ * extension
+ *
+ * @return void* pointer to memory address holding the extension information
+ */
+ PHPCPP_EXPORT void *get_module()
+ {
+ // create an instance of the Php::Extension class
+ static Php::Extension extension("bubblesort", "1.0");
+
+ // add the bubblesort function to the extension
+ extension.add("native_bubblesort", native_bubblesort);
+
+ // return the extension
+ return extension;
+ }
+}
+
+</code></pre>
+</p>