Exemplo n.º 1
0
 /**
  *Bubble sort: A very naive sort that keeps swapping elements until the container is sorted.
  *Requirements: Needs to be able to compare elements with <=>, and the [] []= methods should
  *be implemented for the container.
  *Time Complexity: О(n^2)
  *Space Complexity: О(n) total, O(1) auxiliary
  *Stable: Yes
  *
  *@param array $items Array to be sorted
  *@return array
  */
 public static function bubble_sort($items)
 {
     $size = count($items);
     for ($i = 0; $i < $size; $i++) {
         for ($j = 0; $j < $size - 1 - $i; $j++) {
             if ($items[$j + 1] < $items[$j]) {
                 Sort::array_swap($items, $j, $j + 1);
             }
         }
     }
     return $items;
 }