Home  >  Article  >  Backend Development  >  Bubble sorting and array intersection php method_PHP tutorial

Bubble sorting and array intersection php method_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:59:07854browse

Bubble sort-php tutorial version-exchange sort method

$a=array('11','2','13','4','22');
$num = count($a);
for($i=0;$i<$num;$i++){
for($j=0;$j<$num;$j++){
If($a[$i]<$a[$j]){
               $temp = $a[$i];
              $a[$i]=$a[$j];
                $a[$j]=$temp;
}
}  
}
print_r($a);

Merge sort-array intersection-php version

$a=array('1','2','3','4','22');
$b=array('1','3','4','11','22','23');
f($a, $b, 5, 6, $t);
print_r($t);
function f(&$a, &$b, $n, $m, &$t){
$i=0;$j=0;
While($i<$n && $j<$m){
         
If($a[$i]==$b[$j]){
                  echo $a[$i]." ";//Intersection
               $t[]=$a[$i++];
               $t[]=$b[$j++];
          }elseif($a[$i]>$b[$j]){
                $t[]=$b[$j++];

}else{
               $t[]=$a[$i++];

}
         
}
; while($i<$n){
           $t[]=$a[$i++];
}
 
While($j<$m){
           $t[]=$b[$j++];
}
 
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631337.htmlTechArticleBubble sort-php tutorial version-exchange sort method $a=array('11','2', '13','4','22'); $num = count($a); for($i=0;$i$num;$i++){ for($j=0;$j$num; $j++){ if($a[$i]$a[$j]){ $temp = $a[$i]; $a[...
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