Site icon T4Tutorials.com

Insertion Sort Program with array in PHP

Insertion Sort Program with the array in PHP

In this tutorial, we will try to cover the following programs;

  1. Insertion Sort Progam in PHP with a while loop
  2. Insertion Sort Progam in PHP with a do while loop
  3. Insertion Sort Progam with form values entered by the user (while loop)
  4. Insertion Sort Progam with form values entered by the user (do while loop)

Insertion Sort Progam in PHP with a while loop

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; 
charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>
<?php

$t4_arr = array(5,9,14,8,7,19);
  $i = 0;
  While($i < 6)
  {   
    echo $t4_arr[$i]." "; 
	$i++ ;
  }  
  echo "<br/>";  

$i=0;
While($i<6-1)
{
$t4_temp=$t4_arr[$i];
$j=$i-1;
	while(($t4_temp < $t4_arr[$j])&&($j>=0))
	{
	$t4_arr[$j+1]=$t4_arr[$j];
	$j=$j-1;
	}
	$t4_arr[$j+1]=$t4_temp;
	$i++;
	}

echo "sorted list by insertion sort:";
$i=0;
While($i<6)
{
	echo $t4_arr[$i] . " ";
	$i++;
}  
?> 

Insertion Sort Progam in PHP with a while loop

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>
<?php

$t4_arr = array(5,9,14,8,7,19);
  $i = 0;
  Do
  {   
    echo $t4_arr[$i]." "; 
	$i++ ;
  } While($i < 6); 
  echo "<br/>";  

$i=0;
Do
{
$t4_temp=$t4_arr[$i];
$j=$i-1;
	while(($t4_temp < $t4_arr[$j])&&($j>=0))
	{
	$t4_arr[$j+1]=$t4_arr[$j];
	$j=$j-1;
	}
	$t4_arr[$j+1]=$t4_temp;
	$i++;
	}While($i<6-1);

echo "sorted list by insertion sort:";
$i=0;
Do
{
	echo $t4_arr[$i] . " ";
	$i++;
}While($i<6);

  
?> 

Insertion Sort Progam with form values entered by the user (while loop)

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form method ="Post">

Matrix <br>
<input type="text" name="1">
<input type="text" name="2">
<input type="text" name="3">
<input type="text" name="4">
<input type="text" name="5">
<input type="text" name="6"><br>
<input type ="submit" name="ins" value="Insert">

</form>
</body>
</html>

<?php
$t4_arr = array(6);
if (isset($_POST['ins']))
{
$t4_arr[0]=$_POST['1'];
$t4_arr[1]=$_POST['2'];
$t4_arr[2]=$_POST['3'];
$t4_arr[3]=$_POST['4'];
$t4_arr[4]=$_POST['5'];
$t4_arr[5]=$_POST['6'];
for ($i = 0; $i < 6; $i++)   
    echo $t4_arr[$i]." ";
	echo "<br>";
	
$i=0;
while($i<6-1)
{
$t4_temp=$t4_arr[$i];
$j=$i-1;
	while(($t4_temp < $t4_arr[$j])&&($j>=0))
	{
	$t4_arr[$j+1]=$t4_arr[$j];
	$j=$j-1;
	}
	$i++;
	$t4_arr[$j+1]=$t4_temp;
	
	}

echo "sorted list by insertion sort:";
for($i=0;$i<6;$i++)
{
	echo $t4_arr[$i] . " ";
}  
}	
?>

Output

Insertion Sort Progam with form values entered by the user (do while loop)

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form method ="Post">

Matrix <br>
<input type="text" name="1">
<input type="text" name="2">
<input type="text" name="3">
<input type="text" name="4">
<input type="text" name="5">
<input type="text" name="6"><br>
<input type ="submit" name="ins" value="Insert">

</form>
</body>
</html>

<?php
$t4_arr = array(6);
if (isset($_POST['ins']))
{
$t4_arr[0]=$_POST['1'];
$t4_arr[1]=$_POST['2'];
$t4_arr[2]=$_POST['3'];
$t4_arr[3]=$_POST['4'];
$t4_arr[4]=$_POST['5'];
$t4_arr[5]=$_POST['6'];
for ($i = 0; $i < 6; $i++)   
    echo $t4_arr[$i]." ";
	echo "<br>";
	
$i=0;
Do
{
$t4_temp=$t4_arr[$i];
$j=$i-1;
	while(($t4_temp < $t4_arr[$j])&&($j>=0))
	{
	$t4_arr[$j+1]=$t4_arr[$j];
	$j=$j-1;
	}
	$i++;
	$t4_arr[$j+1]=$t4_temp;
	
	}while($i<6-1);

echo "sorted list by insertion sort:";
$i=0;
Do
{
	echo $t4_arr[$i] . " ";
	$i++;
} while($i<6);
}	
?>

Topic Covered

Insertion Sort Program with the array in PHP.

Exit mobile version