In this tutorial I show how to implement a Quicksort algorithm in PHP.
Code:
<?php
$arr = generateArr();
print("UNSORTED ARRAY:<br />");
print_r($arr);
print("<br />SORTED ARRAY:<br />");
quickSort($arr, 0, sizeof($arr) - 1);
print_r($arr);
function generateArr()
{
$a = [];
for ($i = 0; $i < 10; $i++) {
$a[] = rand(0, 100);
}
return $a;
}
function quickSort(&$array, $lowOrig, $highOrig)
{
if (sizeof($array) < 2) {
return;
}
$pivot = $array[($lowOrig + $highOrig) / 2];
$low = $lowOrig;
$high = $highOrig;
while ($low <= $high) {
while ($array[$low] < $pivot) {
$low++;
}
while ($array[$high] > $pivot) {
$high--;
}
if ($low <= $high) {
// Swap their values
$temp = $array[$low];
$array[$low] = $array[$high];
$array[$high] = $temp;
$low++;
$high--;
}
}
if ($lowOrig < $high) {
quickSort($array, $lowOrig, $high);
}
if ($low < $highOrig) {
quickSort($array, $low, $highOrig);
}
}
hi sir
i watched your videos about php and netbeans
but i got problem at the beginning when i write localhost at my browser all i get is xampp page ?
nothing else i tried to change my browser but does not work !
can you help me please ?!
this is my e-mail
helly12@windowslive.com
thanx in advance
Hi,
You need to go to http://localhost/(directory under C:\xampp\htdocs where you have put the web page)
it’s work now
i don’t know how to thank u
Brilliant! No problem 🙂
You’ve got it in one. Codnlu’t have put it better.
sorry sir,
i get new error
i have Alarm triangle in NetBeans when i put the mouse on it there is message appear its content is :
(Do not access superglobal $_POST array directly
use some filtering function instead (e.g. filter_input, condition with is_*() function , etc)
Hi,
So this is due to a security restriction. Reference this manual: http://php.net/manual/en/function.filter-input.php
Whenever you see things like:
$var = $_POST['data'];
In your code, you’d change this to:
$var = filter_input(INPUT_POST, 'data');
I hope that helps.