Skip to content

Instantly share code, notes, and snippets.

@lbj96347
Created May 12, 2012 17:36
Show Gist options
  • Save lbj96347/2667835 to your computer and use it in GitHub Desktop.
Save lbj96347/2667835 to your computer and use it in GitHub Desktop.
php Bubble Sort
<?php
//print_r(time());
function make_array(){
$one_array = array();
for( $i = 1 ; $i < 5000 ; $i++){
$rand_num = rand(0, 5000);
$one_array[$i] = $rand_num;
if ( $i == 1 ){
$time1 = time();
print_r('time1:'.$time1);
}
}
//begin to run buddle function
//print_r($one_array);
return run_buddle($one_array,$time1);
}
function run_buddle($one_array,$time1){
//print_r($one_array);
$length = count($one_array);
echo "<br />";
print_r('array length:'.$length);
for($i = 0 ; $i <= $length - 1; $i++){
for($j = $length - 1; $j >= 1 ; $j--){
if($one_array[$j] < $one_array[$j - 1]){
$temp = $one_array[$j];
$one_array[$j] = $one_array[$j - 1];
$one_array[$j - 1] = $temp;
}
}
if ( $i == $length - 1 ){
$time2 = time();
echo "<br />";
$time_stamp = $time2 - $time1;
print_r("time2 - time1 :".$time_stamp);
}
}
}
make_array();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment