Home > php教程 > php手册 > 解读PHP冒泡排序技巧

解读PHP冒泡排序技巧

WBOY
Release: 2016-06-13 11:12:04
Original
760 people have browsed it

PHP冒泡排序程序文件sort_bubble_up.php
 

  1.  html> 
  2.  head> title>PHP冒泡排序法演示 /title> head> 
  3.  link rel="stylesheet" href="sort.css"> 
  4.  body> 
  5.  h1>冒泡排序法演示 /h1> 
  6.  table cellpadding="5" cellspacing="1" 
    border="0" align="center"> 
  7.  ?php  
  8. //随机生成数组  
  9. $arr=array();  
  10. echo ' tr> td colspan="10" class="title">
    初始值 /td> /tr>';  
  11. echo ' tr>';  
  12. for($i=0;$i 10;$i++){  
  13. $arr[$i]=rand();  
  14. echo " td>$arr[$i]={$arr[$i]} /td>";  
  15. }  
  16. //进行冒泡法排序  
  17. for($i=9;$i>0;$i--){  
  18. echo ' tr> td colspan="10" class="title">第'
    .(10-$i).'次
     /td> /tr>';  
  19. for($j=0;$j $i;$j++){  
  20. if($arr[$j] $arr[$j+1]){  
  21. $tmp=$arr[$j];  
  22. $arr[$j]=$arr[$j+1];  
  23. $arr[$j+1]=$tmp;  
  24. }   
  25. echo ' tr>';  
  26. for($k=0;$k 10;$k++){  
  27. switch($k){   
  28. case $j : echo ' td class="base">'; break;  
  29. case $j+1 : echo ' td class="light">'; break;  
  30. default : echo ' td>';  
  31. }  
  32. echo "$arr[$k]={$arr[$k]} /td>";  
  33. }  
  34. echo ' /tr>';  
  35. }  
  36. }  
  37. //显示排序结果  
  38. echo ' tr> td colspan="10" class="title">结果 /td> /tr>';  
  39. echo ' tr>';  
  40. for($i=0;$i 10;$i++){  
  41. echo " td>$arr[$i]={$arr[$i]} /td>";  
  42. }  
  43. echo ' /tr>';  
  44. ?> 
  45.  /table> 
  46.  /body> /html> 

PHP冒泡排序样式表文件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;}
 


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template