Saturday, January 9, 2016

Merge Sort in PHP

A simple program that I wrote in PHP to sort a series of numbers Merge Sort algorithm.

 If you have some questions please send me an email at jakerpomperada@gmail.comand jakerpomperada@yahoo.com.  

My mobile number here in the Philippines is 09173084360.



Sample Program Output


Program Listing

<?php
error_reporting(0);

$a= $_POST['val_1'];
$b= $_POST['val_2'];
$c= $_POST['val_3'];
$d= $_POST['val_4'];
$e= $_POST['val_5']; 

 if(isset($_POST['clear']))
$a=""; $b=""; $c=""; $d=""; $e="";
$display_original="";  $process_results="";
}
?>
<html>
<head>
<title>Merge Sort in PHP</title>
<style>
body { font-family:arial;}
form { width: 400px; }
label { float: left; width: 95px; }
.clear { clear: both; height: 0; line-height: 0; }
.floatright { float: right; }
</style>
</head>
<body>
<h2>Merge Sort in PHP</h2>
<form  action="" method="post">
<label>Value No. 1 : </label> <input type="text" name="val_1" value="<?php echo $a; ?>" size="5" maxlength="5"  autofocus/><br/><br>
<label>Value No. 2 : </label> <input type="text" name="val_2" value="<?php echo $b; ?>" size="5" maxlength="5"/><br/><br>
<label>Value No. 3 : </label> <input type="text" name="val_3" value="<?php echo $c; ?>" size="5" maxlength="5"/><br/><br>
<label>Value No. 4 : </label> <input type="text" name="val_4" value="<?php echo $d; ?>"size="5" maxlength="5"/><br/><br>
<label>Value No. 5 : </label> <input type="text" name="val_5" value="<?php echo $e; ?>"size="5" maxlength="5"/><br/>
<br />
<input type="submit" value="Sort" name="bubble"/>
<input type="submit" value="Clear" name="clear" />
</form>

<?php
function Merge(&$data, $left, $mid, $right) {
$n1 = $mid - $left + 1;
$n2 = $right - $mid;

for ($i = 0; $i < $n1; $i++)
$L[$i] = $data[$left + $i];

for ($j = 0; $j < $n2; $j++)
$R[$j] = $data[$mid + 1 + $j];

$i = 0;
$j = 0;
$k = $left;

while ($i < $n1 && $j < $n2)
{
if ($L[$i] <= $R[$j])
{
$data[$k] = $L[$i];
$i++;
}
else
{
$data[$k] = $R[$j];
$j++;
}

$k++;
}

while ($i < $n1)
{
$data[$k] = $L[$i];
$i++;
$k++;
}

while ($j < $n2)
{
$data[$k] = $R[$j];
$j++;
$k++;
}

$L = null;
$R = null;
}

function MergeSort(&$data, $count) {
for ($currentSize = 1; $currentSize <= $count - 1; $currentSize = 2 * $currentSize)
{
for ($leftStart = 0; $leftStart < $count - 1; $leftStart += 2 * $currentSize)
{
$mid = $leftStart + $currentSize - 1;
$rightEnd = min($leftStart + 2 * $currentSize - 1, $count - 1);

Merge($data, $leftStart, $mid, $rightEnd);
}
}
}


if(isset($_POST['bubble']))


 $arr = array($a,$b,$c,$d,$e);
  $orig = implode(",", $arr); 

 MergeSort($arr,5);
 $final_sort = implode(",", $arr);

  $display_original =  "Original Values : " .$orig."<br><br>";
  $process_results  = "Sorted Values : " .$final_sort."<br>";
  
  echo $display_original;
  echo $process_results;

}

?>
</body>
</html>





Insertion Sort in PHP

A simple program that I wrote in PHP to sort a series of numbers Insertion Sort algorithm.

 If you have some questions please send me an email at jakerpomperada@gmail.comand jakerpomperada@yahoo.com.  

My mobile number here in the Philippines is 09173084360.



Sample Program Output


Program Listing

<?php
error_reporting(0);

$a= $_POST['val_1'];
$b= $_POST['val_2'];
$c= $_POST['val_3'];
$d= $_POST['val_4'];
$e= $_POST['val_5']; 

 if(isset($_POST['clear']))
$a=""; $b=""; $c=""; $d=""; $e="";
$display_original="";  $process_results="";
}


?>
<html>
<head>
<title>Insertion Sort in PHP</title>
<style>
body { font-family:arial;}
form { width: 400px; }
label { float: left; width: 95px; }
.clear { clear: both; height: 0; line-height: 0; }
.floatright { float: right; }
</style>
</head>
<body>
<h2>Insertion Sort in PHP</h2>
<form  action="" method="post">
<label>Value No. 1 : </label> <input type="text" name="val_1" value="<?php echo $a; ?>" size="5" maxlength="5"  autofocus/><br/><br>
<label>Value No. 2 : </label> <input type="text" name="val_2" value="<?php echo $b; ?>" size="5" maxlength="5"/><br/><br>
<label>Value No. 3 : </label> <input type="text" name="val_3" value="<?php echo $c; ?>" size="5" maxlength="5"/><br/><br>
<label>Value No. 4 : </label> <input type="text" name="val_4" value="<?php echo $d; ?>"size="5" maxlength="5"/><br/><br>
<label>Value No. 5 : </label> <input type="text" name="val_5" value="<?php echo $e; ?>"size="5" maxlength="5"/><br/>
<br />
<input type="submit" value="Sort" name="bubble"/>
<input type="submit" value="Clear" name="clear" />
</form>

<?php
function InsertionSort($array) {

    $sortedArray = array();

    for ($i = 0 ; $i < count($array); $i++) {

        $element = $array[$i];
        $j = $i;

        while($j > 0 && $sortedArray[$j-1] > $element) {

            $sortedArray[$j] = $sortedArray[$j-1];
            $j = $j-1;
        }
        $sortedArray[$j] = $element;
    }
    return $sortedArray;
}

if(isset($_POST['bubble']))


 $arr = array($a,$b,$c,$d,$e);
  $orig = implode(",", $arr); 
  
 $a =InsertionSort($arr);
 $final_sort = implode(",", $a);

  $display_original =  "Original Values : " .$orig."<br><br>";
  $process_results  = "Sorted Values : " .$final_sort."<br>";
  
  echo $display_original;
  echo $process_results;

}

?>
</body>
</html>



Heap Sort in PHP

A simple program that I wrote in PHP to sort a series of numbers Heap Sort algorithm.

 If you have some questions please send me an email at jakerpomperada@gmail.comand jakerpomperada@yahoo.com.  

My mobile number here in the Philippines is 09173084360.


Sample Program Output

Program Listing


<?php
error_reporting(0);
$a= $_POST['val_1'];
$b= $_POST['val_2'];
$c= $_POST['val_3'];
$d= $_POST['val_4'];
$e= $_POST['val_5']; 

 if(isset($_POST['clear']))
$a=""; $b=""; $c=""; $d=""; $e="";
$display_original="";  $process_results="";
}

?>
<html>
<head>
<title>Heap Sort in PHP</title>
<style>
body { font-family:arial;}
form { width: 400px; }
label { float: left; width: 95px; }
.clear { clear: both; height: 0; line-height: 0; }
.floatright { float: right; }
</style>
</head>
<body>
<h2>Heap Sort in PHP</h2>
<form  action="" method="post">
<label>Value No. 1 : </label> <input type="text" name="val_1" value="<?php echo $a; ?>" size="5" maxlength="5"  autofocus/><br/><br>
<label>Value No. 2 : </label> <input type="text" name="val_2" value="<?php echo $b; ?>" size="5" maxlength="5"/><br/><br>
<label>Value No. 3 : </label> <input type="text" name="val_3" value="<?php echo $c; ?>" size="5" maxlength="5"/><br/><br>
<label>Value No. 4 : </label> <input type="text" name="val_4" value="<?php echo $d; ?>"size="5" maxlength="5"/><br/><br>
<label>Value No. 5 : </label> <input type="text" name="val_5" value="<?php echo $e; ?>"size="5" maxlength="5"/><br/>
<br />
<input type="submit" value="Sort" name="bubble"/>
<input type="submit" value="Clear" name="clear" />
</form>

<?php

function build_heap(&$array, $i, $t){
  $tmp_var = $array[$i];    
  $j = $i * 2 + 1;

  while ($j <= $t)  {
   if($j < $t)
    if($array[$j] < $array[$j + 1]) {
     $j = $j + 1; 
    }
   if($tmp_var < $array[$j]) {
    $array[$i] = $array[$j];
    $i = $j;
    $j = 2 * $i + 1;
   } else {
    $j = $t + 1;
   }
  }
  $array[$i] = $tmp_var;
 }

 function heap_sort(&$array) {
  //This will heapify the array
  $init = (int)floor((count($array) - 1) / 2);
  // Thanks jimHuang for bug report
  for($i=$init; $i >= 0; $i--){
   $count = count($array) - 1;
   build_heap($array, $i, $count);
  }

  //swaping of nodes
  for ($i = (count($array) - 1); $i >= 1; $i--)  {
   $tmp_var = $array[0];
   $array [0] = $array [$i];
   $array [$i] = $tmp_var;
   build_heap($array, 0, $i - 1);
  }
 }

    
if(isset($_POST['bubble']))

  $arr = array($a,$b,$c,$d,$e);
  $orig = implode(",", $arr); 
 $result = heap_sort($arr);
 $final_sort = implode(",", $arr);

  $display_original =  "Original Values : " .$orig."<br><br>";
  $process_results  = "Sorted Values : " .$final_sort."<br>";
  
  echo $display_original;
  echo $process_results;

}

?>
</body>
</html>


Counting Sort in PHP

A simple program that I wrote in PHP to sort a series of numbers using PHP using Counting Sort algorithm.

 If you have some questions please send me an email at jakerpomperada@gmail.comand jakerpomperada@yahoo.com.  

My mobile number here in the Philippines is 09173084360.


Sample Program Output


Program Listing

<?php
error_reporting(0);

$a= $_POST['val_1'];
$b= $_POST['val_2'];
$c= $_POST['val_3'];
$d= $_POST['val_4'];
$e= $_POST['val_5']; 

 if(isset($_POST['clear']))
$a=""; $b=""; $c=""; $d=""; $e="";
$display_original="";  $process_results="";
}
?>
<html>
<head>
<title>Counting Sort in PHP</title>
<style>
body { font-family:arial;}
form { width: 400px; }
label { float: left; width: 95px; }
.clear { clear: both; height: 0; line-height: 0; }
.floatright { float: right; }
</style>
</head>
<body>
<h2>Counting Sort in PHP</h2>
<form  action="" method="post">
<label>Value No. 1 : </label> <input type="text" name="val_1" value="<?php echo $a; ?>" size="5" maxlength="5"  autofocus/><br/><br>
<label>Value No. 2 : </label> <input type="text" name="val_2" value="<?php echo $b; ?>" size="5" maxlength="5"/><br/><br>
<label>Value No. 3 : </label> <input type="text" name="val_3" value="<?php echo $c; ?>" size="5" maxlength="5"/><br/><br>
<label>Value No. 4 : </label> <input type="text" name="val_4" value="<?php echo $d; ?>"size="5" maxlength="5"/><br/><br>
<label>Value No. 5 : </label> <input type="text" name="val_5" value="<?php echo $e; ?>"size="5" maxlength="5"/><br/>
<br />
<input type="submit" value="Sort" name="bubble"/>
<input type="submit" value="Clear" name="clear" />
</form>

<?php
function counting_sort(&$data, $n, $min, $max) {
$cLen = $max - $min + 1;
$z = 0;

for ($i = 0; $i < $cLen; $i++)
$count[$i] = 0;

for ($i = 0; $i < $n; $i++)
$count[$data[$i] - $min]++;

for ($i = $min; $i <= $max; $i++)
{
while ($count[$i - $min]-- > 0)
{
$data[$z] = $i;
$z++;
}
}

$count = null;
}


if(isset($_POST['bubble']))


 $arr = array($a,$b,$c,$d,$e);
  $orig = implode(",", $arr); 

 counting_sort($arr,5,-5000,5000);
 $final_sort = implode(",", $arr);

  $display_original =  "Original Values : " .$orig."<br><br>";
  $process_results  = "Sorted Values : " .$final_sort."<br>";
  
  echo $display_original;
  echo $process_results;

}

?>
</body>
</html>



Comb Sort in PHP

A simple program that I wrote in PHP to sort a series of numbers using PHP using Comb Sort algorithm.

 If you have some questions please send me an email at jakerpomperada@gmail.com and jakerpomperada@yahoo.com.  

My mobile number here in the Philippines is 09173084360.


Sample Program Output


Program Listing

<?php
error_reporting(0);

$a= $_POST['val_1'];
$b= $_POST['val_2'];
$c= $_POST['val_3'];
$d= $_POST['val_4'];
$e= $_POST['val_5']; 

 if(isset($_POST['clear']))

$a=""; $b=""; $c=""; $d=""; $e="";
$display_original="";  $process_results="";
}
?>
<html>
<head>
<title>Comb Sort in PHP</title>
<style>
body { font-family:arial;}
form { width: 400px; }
label { float: left; width: 95px; }
.clear { clear: both; height: 0; line-height: 0; }
.floatright { float: right; }
</style>
</head>
<body>
<h2>Comb Sort in PHP</h2>
<form  action="" method="post">
<label>Value No. 1 : </label> <input type="text" name="val_1" value="<?php echo $a; ?>" size="5" maxlength="5"  autofocus/><br/><br>
<label>Value No. 2 : </label> <input type="text" name="val_2" value="<?php echo $b; ?>" size="5" maxlength="5"/><br/><br>
<label>Value No. 3 : </label> <input type="text" name="val_3" value="<?php echo $c; ?>" size="5" maxlength="5"/><br/><br>
<label>Value No. 4 : </label> <input type="text" name="val_4" value="<?php echo $d; ?>"size="5" maxlength="5"/><br/><br>
<label>Value No. 5 : </label> <input type="text" name="val_5" value="<?php echo $e; ?>"size="5" maxlength="5"/><br/>
<br />
<input type="submit" value="Sort" name="bubble"/>
<input type="submit" value="Clear" name="clear" />
</form>

<?php
function CombSort(&$data, $count) {
$gap = $count;
$swaps = true;

while ($gap > 1 || $swaps)
{
$gap /= 1.247330950103979;

if ($gap < 1)
$gap = 1;

$i = 0;
$swaps = false;

while ($i + $gap < $count)
{
$igap = $i + $gap;

if ($data[$i] > $data[$igap])
{
$temp = $data[$i];
$data[$i] = $data[$igap];
$data[$igap] = $temp;
$swaps = true;
}

$i++;
}
}
}

if(isset($_POST['bubble']))



 $arr = array($a,$b,$c,$d,$e);
  $orig = implode(",", $arr); 
  
 Combsort($arr,5);
 $final_sort = implode(",", $arr);

  $display_original =  "Original Values : " .$orig."<br><br>";
  $process_results  = "Sorted Values : " .$final_sort."<br>";
  
  echo $display_original;
  echo $process_results;

}

?>
</body>
</html>

Bucket Sort in PHP

A sample program that I wrote to sort a series of numbers using PHP using bucket sort algorithm.

 If you have some questions please send me an email at jakerpomperada@gmail.com and jakerpomperada@yahoo.com.  

My mobile number here in the Philippines is 09173084360.




Sample Program Output


Program Listing 

<?php
error_reporting(0);

$a= $_POST['val_1'];
$b= $_POST['val_2'];
$c= $_POST['val_3'];
$d= $_POST['val_4'];
$e= $_POST['val_5']; 

 if(isset($_POST['clear']))
$a=""; $b=""; $c=""; $d=""; $e="";
$display_original="";  $process_results="";
}
?>
<html>
<head>
<title>Bucket in PHP</title>
<style>
body { font-family:arial;}
form { width: 400px; }
label { float: left; width: 95px; }
.clear { clear: both; height: 0; line-height: 0; }
.floatright { float: right; }
</style>
</head>
<body>
<h2>Bucket in PHP</h2>
<form  action="" method="post">
<label>Value No. 1 : </label> <input type="text" name="val_1" value="<?php echo $a; ?>" size="5" maxlength="5"  autofocus/><br/><br>
<label>Value No. 2 : </label> <input type="text" name="val_2" value="<?php echo $b; ?>" size="5" maxlength="5"/><br/><br>
<label>Value No. 3 : </label> <input type="text" name="val_3" value="<?php echo $c; ?>" size="5" maxlength="5"/><br/><br>
<label>Value No. 4 : </label> <input type="text" name="val_4" value="<?php echo $d; ?>"size="5" maxlength="5"/><br/><br>
<label>Value No. 5 : </label> <input type="text" name="val_5" value="<?php echo $e; ?>"size="5" maxlength="5"/><br/>
<br />
<input type="submit" value="Sort" name="bubble"/>
<input type="submit" value="Clear" name="clear" />
</form>

<?php
function BucketSort(&$data)
{
$minValue = $data[0];
$maxValue = $data[0];
$dataLength = count($data);

for ($i = 1; $i < $dataLength; $i++)
{
if ($data[$i] > $maxValue)
$maxValue = $data[$i];
if ($data[$i] < $minValue)
$minValue = $data[$i];
}

$bucket = array();
$bucketLength = $maxValue - $minValue + 1;
for ($i = 0; $i < $bucketLength; $i++)
{
$bucket[$i] = array();
}

for ($i = 0; $i < $dataLength; $i++)
{
array_push($bucket[$data[$i] - $minValue], $data[$i]);
}
$k = 0;
for ($i = 0; $i < $bucketLength; $i++)
{
$bucketCount = count($bucket[$i]);
if ($bucketCount > 0)
{
for ($j = 0; $j < $bucketCount; $j++)
{
$data[$k] = $bucket[$i][$j];
$k++;
}
}
}
}

if(isset($_POST['bubble']))


 $arr = array($a,$b,$c,$d,$e);
  $orig = implode(",", $arr); 

 BucketSort($arr);
 $final_sort = implode(",", $arr);

  $display_original =  "Original Values : " .$orig."<br><br>";
  $process_results  = "Sorted Values : " .$final_sort."<br>";
  
  echo $display_original;
  echo $process_results;

}

?>
</body>
</html>



Bubble Sort in PHP

A sample program that I wrote in PHP to perform bubble sort algorithm.

 If you have some questions please send me an email at jakerpomperada@gmail.com and jakerpomperada@yahoo.com.  

My mobile number here in the Philippines is 09173084360.


Sample Program Output

Program Listing

<?php
error_reporting(0);
$a= $_POST['val_1'];
$b= $_POST['val_2'];
$c= $_POST['val_3'];
$d= $_POST['val_4'];
$e= $_POST['val_5']; 

 if(isset($_POST['clear']))
$a=""; $b=""; $c=""; $d=""; $e="";
$display_original="";  $process_results="";
}

?>
<html>
<head>
<title>Bubble Sort in PHP</title>
<style>
body { font-family:arial;}
form { width: 400px; }
label { float: left; width: 95px; }
.clear { clear: both; height: 0; line-height: 0; }
.floatright { float: right; }
</style>
</head>
<body>
<h2>Bubble Sort in PHP</h2>
<form  action="" method="post">
<label>Value No. 1 : </label> <input type="text" name="val_1" value="<?php echo $a; ?>" size="5" maxlength="5"  autofocus/><br/><br>
<label>Value No. 2 : </label> <input type="text" name="val_2" value="<?php echo $b; ?>" size="5" maxlength="5"/><br/><br>
<label>Value No. 3 : </label> <input type="text" name="val_3" value="<?php echo $c; ?>" size="5" maxlength="5"/><br/><br>
<label>Value No. 4 : </label> <input type="text" name="val_4" value="<?php echo $d; ?>"size="5" maxlength="5"/><br/><br>
<label>Value No. 5 : </label> <input type="text" name="val_5" value="<?php echo $e; ?>"size="5" maxlength="5"/><br/>
<br />
<input type="submit" value="Sort" name="bubble"/>
<input type="submit" value="Clear" name="clear" />
</form>

<?php

function Bubble_Sort(array $arr)
{
    $n = sizeof($arr);    
    for ($i = 1; $i < $n; $i++) {
        $flag = false;
        for ($j = $n - 1; $j >= $i; $j--) {
            if($arr[$j-1] > $arr[$j]) {
                $tmp = $arr[$j - 1];
                $arr[$j - 1] = $arr[$j];
                $arr[$j] = $tmp;
                $flag = true;
            }
        }
        if (!$flag) {
            break;
        }
    }
     
    return $arr;
}

    
if(isset($_POST['bubble']))

  $arr = array($a,$b,$c,$d,$e);
  $result = Bubble_Sort($arr);

  $original_values = implode(",", $arr);
  $withComma = implode(",", $result);

  $display_original =  "Original Values : " .$original_values."<br><br>";
  $process_results  = "Sorted Values : " .$withComma."<br>";
  
  echo $display_original;
  echo $process_results;

}

?>
</body>
</html>