From 17c723479fc2aa7de69285771c7d91eb51bef288 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Fri, 7 Mar 2014 13:21:55 +0100 Subject: added initial bubblesort example --- documentation/bubblesort.html | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 documentation/bubblesort.html (limited to 'documentation/bubblesort.html') 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 @@ +

How fast is a C++ extension

+

+ Native extensions are fast. But how fast are they? We can demonstrate this + with a very simple extension: bubblesort. +

+

+ 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. +

+

+

+<?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 < $count; $i++)
+    {
+        // loop through the elements that were already processed
+        for ($j = 0; $j < $count - $i; $j++)
+        {
+            // move on if smaller
+            if ($input[$j] <= $input[$j+1]) continue;
+    
+            // swap elements
+            $temp = $input[$j];
+            $input[$j] = $input[$j+1];
+            $input[$j+1] = $temp;
+        }
+    }
+}
+?>
-- 
cgit v1.2.3