Advertisement
rgman

Unión de Arreglos Ordenados

Jul 7th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.57 KB | None | 0 0
  1. <?php
  2.  
  3. $primero = array(0,5,6,9,10,16);
  4. $segundo = array(1,2,3,7,8,12,13,19);
  5. $union = array();
  6.  
  7. $i = 0;
  8. $j = 0;
  9. while ($i < count($primero) && $j < count($segundo)) {
  10.     if ($i === count($primero)) {
  11.         $union[] = $segundo[$j];
  12.         $j++;
  13.         continue;
  14.     }
  15.  
  16.     if ($j === count($segundo)) {
  17.         $union[] = $primero[$i];
  18.         $i++;
  19.         continue;
  20.     }
  21.  
  22.     if ($primero[$i] < $segundo[$j]) {
  23.         $union[] = $primero[$i];
  24.         $i++;
  25.     } else {
  26.         $union[] = $segundo[$j];
  27.         $j++;
  28.     }
  29. }
  30.  
  31. print_r($union);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement