Feb 26

NetBeans PHP Tutorial – Quicksort Algorithm #16

In this tutorial I show how to implement a Quicksort algorithm in PHP.


$arr = generateArr();
print("UNSORTED ARRAY:<br />");
print("<br />SORTED ARRAY:<br />");
quickSort($arr, 0, sizeof($arr) - 1);
function generateArr()
	$a = [];
	for ($i = 0; $i < 10; $i++) {
		$a[] = rand(0, 100);
	return $a;
function quickSort(&$array, $lowOrig, $highOrig)
	if (sizeof($array) < 2) {
	$pivot = $array[($lowOrig + $highOrig) / 2];
	$low = $lowOrig;
	$high = $highOrig;
	while ($low <= $high) {
		while ($array[$low] < $pivot) {
		while ($array[$high] > $pivot) {
		if ($low <= $high) {
			// Swap their values
			$temp = $array[$low];
			$array[$low] = $array[$high];
			$array[$high] = $temp;
	if ($lowOrig < $high) {
		quickSort($array, $lowOrig, $high);
	if ($low < $highOrig) {
		quickSort($array, $low, $highOrig);


Skip to comment form

  1. fares

    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


    thanx in advance

    1. Eddy


      You need to go to http://localhost/(directory under C:\xampp\htdocs where you have put the web page)

  2. fares

    it’s work now
    i don’t know how to thank u

    1. Eddy

      Brilliant! No problem 🙂

    2. Donte

      You’ve got it in one. Codnlu’t have put it better.

  3. fares

    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)

    1. Eddy


      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.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>