Home > Article > Backend Development > Bubble sorting and array intersection php method_PHP tutorial
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++];
}
}