问题描述
我想每1秒仅显示1个比较,以便直观地查看如何逐步对数组进行排序。
我试图在嵌套循环的末尾添加sleep(1),并且页面在不显示任何内容的情况下保持加载状态
<?PHP
$my_array = [100,99,98,97,96];
function bubblesort($my_array){
$comparisons = 0;
$len = count($my_array);
for( $i = 0 ; $i < $len ; $i++){
for( $j = 0,$temp = 0 ; $j < $len - 1 ; $j++ ){
$comparisons++;
if ($my_array[$j] > $my_array[$j+1] ){
$temp = $my_array[$j+1];
$my_array[$j+1] = $my_array[$j];
$my_array[$j] = $temp;
} // end of if conditional
} // end of inner for loop
} // end of first for loop
echo '<h4>' . $comparisons . ' Comparisons</h4>';
return $my_array;
} // end of bubbleSort()
echo '<strong>Before Sorting</strong><br>' . implode( ',',$my_array ) . '<br>';
$sorted = bubbleSort( $my_array );
echo '<strong>After Sorting</strong><br>' . implode( ',$sorted );
?>
有什么想法吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)