Home  >  Article  >  Backend Development  >  PHP bubble sorting and binary search examples_PHP tutorial

PHP bubble sorting and binary search examples_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:57:411004browse

Bubble sorting and binary search sorting algorithms are what we learned in junior high school. Let me introduce the bubble sorting and binary search algorithms in PHP. When looking for examples, please feel free to use them as references.

The code is as follows
 代码如下 复制代码

//冒泡法排序
//随便给出一个乱序数组
$arr = array(0,2,10,9,19,23,89,112,321,234);
//统计数组
$num = count($arr);
//冒泡倒序排列
for($i=0;$i<$num-1;$i++){
for($m=0;$m<$num-1;$m++){
if($arr[$m]<$arr[$m+1]){
$temp = $arr[$m];
$arr[$m] = $arr[$m+1];
$arr[$m+1] = $temp;
}
// echo $arr[$m].'
';
 }
}
//输出排序后的结果
var_dump($arr);
//冒泡顺序排列
for($x=0;$x<$num-1;$x++){
for($y=0;$y<$num-1;$y++){
if($arr[$y]>$arr[$y+1]){
   $temp = $arr[$y];
   $arr[$y] = $arr[$y+1];
   $arr[$y+1] = $temp;
  }
 }
}
//输出排序后的结果
var_dump($arr);
//二分法查找
function dichotomy($array,$k,$low=0,$high=0){
 if(count($array)!= 0 && $high == 0){
  $high = count($array);
 }
 if($low <= $high){
$mid = intval(($low+$high)/2);
if( $array[$mid] == $k ){
return $mid;
}elseif( $k<$array[$mid]){
return dichotomy( $array,$k,$low=0,$mid-1);
}else{
return dichotomy( $array,$k,$mid+1,$high);
}
}else{
return false;
}
}
//输出查找结果
echo dichotomy($arr,23);

Copy code

//Bubble sorting
//Just give a random array
$arr = array(0,2,10,9,19,23,89,112,321,234);
//Statistical array
$num = count($arr);
//Bubbling in reverse order
for($i=0;$i<$num-1;$i++){
for($m=0;$m<$num-1;$m++){
if($arr[$m]<$arr[$m+1]){
$temp = $arr[$m];
$arr[$m] = $arr[$m+1];
$arr[$m+1] = $temp;
}
// echo $arr[$m].'
';
}
}
//Output the sorted results
var_dump($arr);
//Arrange in bubble order
for($x=0;$x<$num-1;$x++){
for($y=0;$y<$num-1;$y++){
if($arr[$y]>$arr[$y+1]){
$temp = $arr[$y];
$arr[$y] = $arr[$y+1];
$arr[$y+1] = $temp;
}
}
}
//Output the sorted results
var_dump($arr);
//Binary search
function dichotomy($array,$k,$low=0,$high=0){
if(count($array)!= 0 && $high == 0){
$high = count($array);
}
if($low <= $high){
$mid = intval(($low+$high)/2);
if( $array[$mid] == $k ){
Return $mid;
}elseif( $k<$array[$mid]){
Return dichotomy( $array,$k,$low=0,$mid-1);
}else{
Return dichotomy( $array,$k,$mid+1,$high);
}
}else{
return false;
}
}
//Output search results
echo dichotomy($arr,23);

Today I briefly studied the most commonly used bubble sorting and binary search, and wrote a simple case to strengthen my learning of PHP

, I also hope it can provide a little help to PHP learners in the future. http://www.bkjia.com/PHPjc/631505.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/631505.html
TechArticle
Bubble sorting and binary search sorting algorithms are what we learned in junior high school. Let me introduce them below. PHP bubble sorting and binary search examples, please feel free to refer to them. Code...
Statement:
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