NetBeans PHP Tutorial – Quicksort Algorithm #16

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);
	}
}

7 thoughts on “NetBeans PHP Tutorial – Quicksort Algorithm #16

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

  2. 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)

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *