1

I have to sort 2d array

   $items = array(
   array(15, 16, 8, 1),
   array(2, 3, 4, 7),
   array(9, 11, 19, 6,)
   );

with bubble sort ant to get something like this

  1,2,3,4
  6,7,8,9
  11,15,16,19

I can't find anywhere bubble sort for multiple arrays. Can you help me?

I tried something like this, but it isn't working:

 $iterations = 0;
 for ($i = 0; $i < count($array); $i++)
 {
   $iterations++;
   $hasSwap = false;
     for ($j = 0; $j < count($array) - 1 - $i; $j++) 
      {
        $iterations++;
            if ($array[$j] > $array[$j + 1]) 
            {
              $hasSwap = true;
              swap($array, $j, $j + 1);
            }
         }

        if (!$hasSwap) 
        {
          break;
        }
   }
   var_dump($iterations);
   print_r($array);
deizi23
  • 35
  • 4

1 Answers1

1

You are treating the three arrays as one. So

  1. Create one array out of them
  2. Use standard bubble sort
  3. Group them into three arrays.
Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41