Beispiel #1
0
 function trimMedianQuickSort(&$array, &$comparator, $head, $tail, $median = 4)
 {
     $median = (int) $median;
     if ($tail - $head > $median) {
         $i = intval(($tail + $head) / 2);
         if ($comparator->compare($array[$head], $array[$i]) > 0) {
             Arrays::swap($array, $head, $i);
         }
         if ($comparator->compare($array[$head], $array[$tail]) > 0) {
             Arrays::swap($array, $head, $tail);
         }
         if ($comparator->compare($array[$i], $array[$tail]) > 0) {
             Arrays::swap($array, $i, $tail);
         }
         $j = $tail - 1;
         Arrays::swap($array, $i, $j);
         $i = $head;
         $v = $array[$j];
         for (;;) {
             $i++;
             while ($comparator->compare($array[$i], $v) < 0) {
                 $i++;
             }
             $j--;
             while ($comparator->compare($array[$j], $v) > 0) {
                 $j--;
             }
             if ($j < $i) {
                 break;
             }
             Arrays::swap($array, $i, $j);
         }
         Arrays::swap($array, $i, $tail - 1);
         Arrays::trimMedianQuickSort($array, $comparator, $head, $j, $median);
         Arrays::trimMedianQuickSort($array, $comparator, $i + 1, $tail, $median);
     } else {
         Arrays::insertSort($array, $comparator, $head, $tail);
     }
 }