Interpretation of PHP Bubble Sorting Techniques_PHP Tutorial

WBOY
Release: 2016-07-15 13:35:15
Original
1067 people have browsed it

PHP bubble sort program file sort_bubble_up.php

  1. < html>
  2. < head>< title>PHP bubble sort demonstration< /title>< head>
  3. < link rel="stylesheet" href="sort.css">
  4. < body>
  5. < h1>Bubble sorting demonstration< /h1>
  6. < table cellpadding="5" cellspacing="1"
    border="0" align="center">
  7. < ?php
  8. //Randomly generate array
  9. $arr=array();
  10. echo '< tr>< td colspan ="10" class="title">
    Initial value< /td>< /tr>'; 🎜>
  11. tr
  12. >'; for($i=0
  13. ;$i
  14. < 10;$i++){ $arr[$i]=rand(); echo "<
  15. td
  16. >$arr[$i]={$arr [$i]}< /td> "; } //Perform bubble sorting for($i
  17. =
  18. 9
  19. ;$i
  20. >0;$i--){ echo '<
  21. tr
  22. >< td colspan ="10" class ="title">th'.(10-$i).'次< /td>< /tr>';  
  23. for($j=0;$j< $i;$j++){  
  24. if($arr[$j]< $arr[$j+1]){  
  25. $tmp=$arr[$j];  
  26. $arr[$j]=$arr[$j+1];  
  27. $arr[$j+1]=$tmp;  
  28. }   
  29. echo '< tr>';  
  30. for($k=0;$k< 10;$k++){  
  31. switch($k){   
  32. case $j : echo '< td class="base">'; break;  
  33. case $j+1 : echo '< td class="light">'; break;  
  34. default : echo '< td>';  
  35. }  
  36. echo "$arr[$k]={$arr[$k]}< /td>";  
  37. }  
  38. echo '< /tr>';  
  39. }  
  40. }
  41. //Display sorting results
  42. echo '< tr>< td colspan="10" class="title">Result< /td>< /tr>'; 🎜>
  43. tr>'; for($i=
  44. 0;$i < 10;$i++){ echo "<
  45. td>$arr[$i]={$arr[$i]} < /td>"; } echo '
  46. <
  47. /tr
  48. >'; >
  49. <
  50. /body>< /html>
  51. PHP bubble sort style sheet file sort.css h1{text-align: center; color: blue;}table{font-size: 12px; font-family: arial; background-color: black; text-align: center;}td{background-color: white;}.base{background-color: #0FF;}.light{background-color: #0DD;}.title{background-color: # 3FF; text-align: center;} http://www.bkjia.com/PHPjc/445918.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445918.html
TechArticle

PHP bubble sort program file sort_bubble_up.php html head title PHP bubble sort method demonstration/title head link rel = stylesheet href = sort.css body h1 bubble sort method demonstration/h1 table cel...




source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!