Ejemplo n.º 1
0
 /**
  * Sort by QuickSort technique
  * FROM: http://martinjansen.com/projects/Quicksort/
  *
  * @param   array   $array        Input array
  * @param  int     $firstElement Element to start the sort
  * @param  int     $lastElement  Element where the sort should stop
  * @return  bool    True if sort was ok, false if not.
  * @access  public
  */
 static function QuickSort(&$array, $firstElement = null, $lastElement = null)
 {
     if (!is_array($array)) {
         return false;
     }
     if (is_null($firstElement)) {
         $firstElement = 0;
     }
     if (is_null($lastElement)) {
         $lastElement = count($array) - 1;
     }
     if ($firstElement < $lastElement) {
         $middleElement = floor(($firstElement + $lastElement) / 2);
         $compareElement = $array[$middleElement];
         $fromLeft = $firstElement;
         $fromRight = $lastElement;
         while ($fromLeft <= $fromRight) {
             while ($array[$fromLeft] < $compareElement) {
                 $fromLeft++;
             }
             while ($array[$fromRight] > $compareElement) {
                 $fromRight--;
             }
             if ($fromLeft <= $fromRight) {
                 Jaws_ArraySort::QuickSortChangeElements($array, $fromLeft, $fromRight);
                 $fromLeft++;
                 $fromRight--;
             }
         }
         Jaws_ArraySort::QuickSort($array, $firstElement, $fromRight);
         Jaws_ArraySort::QuickSort($array, $fromLeft, $lastElement);
     }
     return true;
 }