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;
- Insertion Sort Progam in PHP with a while loop
- Insertion Sort Progam in PHP with a do while loop
- Insertion Sort Progam with form values entered by the user (while loop)
- Insertion Sort Progam with form values entered by the user (do while loop)
Insertion Sort Progam in PHP with a while loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<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.