Пример #1
0
 protected function sort(AbstractSort $sort)
 {
     $swapped = TRUE;
     $i = 0;
     $j = $this->length - 1;
     while ($i < $j && $swapped) {
         $swapped = FALSE;
         for ($k = $i; $k < $j; $k++) {
             if ($this->originalData[$k] > $this->originalData[$k + 1]) {
                 ArraysW::swap($this->originalData, $k, $k + 1);
                 $swapped = TRUE;
             }
         }
         $j--;
         if ($swapped) {
             $swapped = FALSE;
             for ($k = $j; $k > $i; $k--) {
                 if ($this->originalData[$k] < $this->originalData[$k - 1]) {
                     ArraysW::swap($this->originalData, $k, $k - 1);
                     $swapped = TRUE;
                 }
             }
         }
         $i++;
     }
     $this->sortedData = $this->originalData;
 }
Пример #2
0
 protected function sort(AbstractSort $sort)
 {
     for ($i = $this->length / 2 - 1; $i >= 0; $i--) {
         $this->repairTop($this->originalData, $this->length - 1, $i);
     }
     for ($i = $this->length - 1; $i > 0; $i--) {
         ArraysW::swap($this->originalData, 0, $i);
         $this->repairTop($this->originalData, $i - 1, 0);
     }
     $this->sortedData = $this->originalData;
 }
Пример #3
0
 protected function sort(AbstractSort $sort)
 {
     $j = $this->length - 2;
     $swapped = TRUE;
     while ($swapped) {
         $swapped = FALSE;
         for ($i = 0; $i <= $j; $i++) {
             if ($this->originalData[$i] > $this->originalData[$i + 1]) {
                 ArraysW::swap($this->originalData, $i, $i + 1);
                 $swapped = TRUE;
             }
         }
     }
     $this->sortedData = $this->originalData;
 }
Пример #4
0
 protected function sort(AbstractSort $sort)
 {
     for ($i = 1; $i < $this->length;) {
         if ($this->originalData[$i - 1] <= $this->originalData[$i]) {
             $i++;
         } else {
             ArraysW::swap($this->originalData, $i, $i - 1);
             $i--;
             if ($i == 0) {
                 $i = 1;
             }
         }
     }
     $this->sortedData = $this->originalData;
 }
Пример #5
0
 protected function sort(AbstractSort $sort)
 {
     for ($i = 0; $i < $this->length - 1; $i++) {
         $min = $i;
         for ($j = $i + 1; $j < $this->length; $j++) {
             if ($this->originalData[$j] > $this->originalData[$min]) {
                 $min = $j;
             }
         }
         if ($min != $i) {
             // this may help
             ArraysW::swap($this->originalData, $i, $min);
         }
     }
     $this->sortedData = $this->originalData;
 }
Пример #6
0
 protected function sort(AbstractSort $sort)
 {
     $swapped = FALSE;
     $gap = $this->length;
     while ($gap != 1 || $swapped) {
         $gap /= 1.33;
         // 4/3
         $swapped = FALSE;
         if ($gap < 1) {
             $gap = 1;
         }
         for ($i = 0; $i + $gap < $this->length; $i++) {
             if ($this->originalData[$i] < $this->originalData[$i + $gap]) {
                 ArraysW::swap($this->originalData, $i, $i + $gap);
                 $swapped = TRUE;
             }
         }
     }
     $this->sortedData = $this->originalData;
 }
Пример #7
0
 protected function sort(AbstractSort $sort)
 {
     for ($i = 0; $i < $this->length / 2; $i++) {
         $swapped = FALSE;
         for ($j = $i; $j < $this->length - $i - 1; $j++) {
             if ($this->originalData[$j] < $this->originalData[$j + 1]) {
                 ArraysW::swap($this->originalData, $j, $j + 1);
                 $swapped = TRUE;
             }
         }
         for ($j = $this->length - 2 - $i; $j > $i; $j--) {
             if ($this->originalData[$j] > $this->originalData[$j - 1]) {
                 ArraysW::swap($this->originalData, $j, $j - 1);
                 $swapped = TRUE;
             }
         }
         if ($swapped) {
             break;
         }
     }
     $this->sortedData = $this->originalData;
 }
Пример #8
0
 public function merge(BinaryHeap $heap)
 {
     $this->heapify(ArraysW::merge2($this->array, $heap->array));
 }
Пример #9
0
 public function getLongestPalindromeManacher()
 {
     if ($this->isPalindrome()) {
         return $this;
     }
     $s2 = self::addBoundaries($this->toCharArray());
     $s2Length = count($s2);
     $p = [];
     $c = 0;
     $r = 0;
     $m = 0;
     $n = 0;
     for ($i = 1; $i < $s2Length; $i++) {
         if ($i > $r) {
             $p[$i] = 0;
             $m = $i - 1;
             $n = $i + 1;
         } else {
             $i2 = $c * 2 - $i;
             if ($p[$i2] < $r - $i) {
                 $p[$i] = $p[$i2];
                 $m = -1;
             } else {
                 $p[$i] = $r - $i;
                 $n = $r + 1;
                 $m = $i * 2 - $n;
             }
         }
         while ($m >= 0 && $n < $s2Length && $s2[$m] == $s2[$n]) {
             $p[$i]++;
             $m--;
             $n++;
         }
         if ($i + $p[$i] > $r) {
             $c = $i;
             $r = $i + $p[$i];
         }
     }
     $len = 0;
     $c = 0;
     for ($i = 1; $i < $s2Length; $i++) {
         if ($len < $p[$i]) {
             $len = $p[$i];
             $c = $i;
         }
     }
     $ss = ArraysW::copyOfRange($s2, $c - $len, $c + $len + 1);
     return self::arrayToString(self::removeBoundaries($ss));
 }