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;
- Sorting an Array with Selection Sort in PHP using while loop.
- Sorting an Array with Selection Sort in PHP using do while loop.
- Selection Sort the array with form values entered by the user. (using while loop)
- 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
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 | <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
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 | <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)
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | <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)
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | <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.