Site icon T4Tutorials.com

Sorting an Array with Selection Sort in PHP – While loop

Sorting an Array with Selection Sort in PHP – While loop

In this tutorial, we will cover the following programs of PHP;

  1. Sorting an Array with Selection Sort in PHP using while loop.
  2. Sorting an Array with Selection Sort in PHP using do while loop.
  3. Selection Sort the array with form values entered by the user. (using while loop)
  4. Selection Sort the array with form values entered by the user. (using do while loop)

Sorting an Array with Selection Sort in PHP using 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,3,7,21);
  
$i = 0; 
while($i < 6)  
{   
    echo $t4_arr[$i]." ";
	$i++;  
  }  
  echo "<br/>";  

$i=0;
while($i<6-1)
{
$t4_h=$t4_arr[$i];
$t4_l=$i;

	$j=$i+1;
	while($j<6)
	{
		if($t4_h > $t4_arr[$j])
		{
			$t4_h=$t4_arr[$j];
			$t4_l=$j;
		}
		$j++;
	}
	$t4_temp=$t4_arr[$i];
	$t4_arr[$i]=$t4_arr[$t4_l];
	$t4_arr[$t4_l]=$t4_temp;
	$i++;
}
echo "sorted list:";
for($i=0;$i<6;$i++)
{
	echo $t4_arr[$i] . " ";
}
  
?> 

Output

Sorting an Array with Selection Sort in PHP using 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>
</body>
</html>
<?php
$t4_arr = array(5,9,14,3,7,21);
  
$i = 0; 
Do 
{   
    echo $t4_arr[$i]." ";
	$i++;  
  } while($i < 6) ; 
  echo "<br/>";  

$i=0;
Do
{
$t4_h=$t4_arr[$i];
$t4_l=$i;

	$j=$i+1;
	while($j<6)
	{
		if($t4_h > $t4_arr[$j])
		{
			$t4_h=$t4_arr[$j];
			$t4_l=$j;
		}
		$j++;
	}
	$t4_temp=$t4_arr[$i];
	$t4_arr[$i]=$t4_arr[$t4_l];
	$t4_arr[$t4_l]=$t4_temp;
	$i++;
}while($i<6-1);
echo "sorted list:";
for($i=0;$i<6;$i++)
{
	echo $t4_arr[$i] . " ";
}
  
?> 

Selection Sort the array with form values entered by the user (using while loop)

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
input[type="text"] {
    width: 20px;
	height: 20px;
}
</style>
<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'];
$i = 0; 
while($i < 6)  
{   
    echo $t4_arr[$i]." ";
	$i++;  
  }  
  echo "<br/>";  
$i=0;
while($i<6-1)
{
$t4_h=$t4_arr[$i];
$t4_l=$i;

	$j=$i+1;
	while($j<6)
	{
		if($t4_h > $t4_arr[$j])
		{
			$t4_h=$t4_arr[$j];
			$t4_l=$j;
		}
		$j++;
	}
	$t4_temp=$t4_arr[$i];
	$t4_arr[$i]=$t4_arr[$t4_l];
	$t4_arr[$t4_l]=$t4_temp;
	$i++;
}
echo "Sorted list by Selection sort:";
for($i=0;$i<6;$i++)
{
	echo $t4_arr[$i] . " ";
}
  
}  
?>

Output

Selection Sort the array with form values entered by the user (using do while loop)

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
input[type="text"] {
    width: 20px;
	height: 20px;
}
</style>
<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'];
$i = 0; 
Do 
{   
    echo $t4_arr[$i]." ";
	$i++;  
  }while($i < 6) ; 
  echo "<br/>";  
$i=0;
Do
{
$t4_h=$t4_arr[$i];
$t4_l=$i;

	$j=$i+1;
	while($j<6)
	{
		if($t4_h > $t4_arr[$j])
		{
			$t4_h=$t4_arr[$j];
			$t4_l=$j;
		}
		$j++;
	}
	$t4_temp=$t4_arr[$i];
	$t4_arr[$i]=$t4_arr[$t4_l];
	$t4_arr[$t4_l]=$t4_temp;
	$i++;
}while($i<6-1);
echo "Sorted list by Selection sort:";
for($i=0;$i<6;$i++)
{
	echo $t4_arr[$i] . " ";
}
  
}  
?>

Topic Covered

Sorting an Array with Selection Sort in PHP – While loop.

Exit mobile version