本文整理汇总了PHP中quicksort函数的典型用法代码示例。如果您正苦于以下问题:PHP quicksort函数的具体用法?PHP quicksort怎么用?PHP quicksort使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了quicksort函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: quicksort
function quicksort($array)
{
if (count($array) < 2) {
return $array;
}
$left = $right = array();
reset($array);
$pivot_key = key($array);
$pivot = array_shift($array);
foreach ($array as $k => $v) {
if ($v['times_ordered'] > $pivot['times_ordered']) {
$left[$k] = $v;
} else {
$right[$k] = $v;
}
}
return array_merge(quicksort($left), array($pivot_key => $pivot), quicksort($right));
}
示例2: quicksort
/**
* Função de ordenação
* @param array $vet
* @param int $ini Início do Array
* @param in $fim Final do Array (tamanho do array)
*/
function quicksort(&$vet, $ini, $fim)
{
$i = $ini;
$j = $fim;
$dir = 1;
while ($i < $j) {
if ($vet[$i] > $vet[$j]) {
$aux = $vet[$i];
$vet[$i] = $vet[$j];
$vet[$j] = $aux;
$dir = -$dir;
}
if ($dir == 1) {
$j--;
} else {
$i++;
}
}
$k = $i;
if ($ini < $fim) {
quicksort($vet, $ini, $k - 1);
}
if ($i < $fim) {
quicksort($vet, $k + 1, $fim);
}
}
示例3: quicksort
function quicksort(&$array, $l = 0, $r = 0)
{
if ($r === 0) {
$r = count($array) - 1;
}
$i = $l;
$j = $r;
$x = $array[($l + $r) / 2];
do {
while ($array[$i] < $x) {
$i++;
}
while ($array[$j] > $x) {
$j--;
}
if ($i <= $j) {
if ($array[$i] > $array[$j]) {
list($array[$i], $array[$j]) = array($array[$j], $array[$i]);
}
$i++;
$j--;
}
} while ($i <= $j);
if ($i < $r) {
quicksort($array, $i, $r);
}
if ($j > $l) {
quicksort($array, $l, $j);
}
}
示例4: quicksort
function quicksort($array)
{
// find array size
$length = count($array);
// base case test, if array of length 0 then just return array to caller
if ($length <= 1) {
return $array;
} else {
// select the last item to act as our pivot point
$pivot = $array[$length - 1];
// declare our two arrays to act as partitions
$left = array();
$right = array();
// loop and compare each item in the array to the pivot value, place item in appropriate partition
for ($i = 0; $i < $length - 1; $i++) {
if ($array[$i] < $pivot) {
$left[] = $array[$i];
} else {
$right[] = $array[$i];
}
}
// use recursion to now sort the left and right lists
return array_merge(quicksort($left), array($pivot), quicksort($right));
}
}
示例5: quicksort
function quicksort(&$A, $p, $r)
{
if ($p < $r) {
$q = partition($A, $p, $r);
quicksort($A, $p, $q - 1);
quicksort($A, $q + 1, $r);
}
}
示例6: testArrayWithOrderElementsReverOrder
public function testArrayWithOrderElementsReverOrder()
{
$original = [9, 8, 7, 6, 5, 4, 3, 2, 1];
$copy = [9, 8, 7, 6, 5, 4, 3, 2, 1];
// Order the array copy
sort($copy, SORT_NUMERIC);
$this->assertEquals(quicksort($original), $copy);
}
示例7: quicksort
function quicksort(&$array, $left, $right)
{
if ($left >= $right) {
return;
}
$pivot = partition($array, $left, $right);
quicksort($array, $pivot + 1, $right);
quicksort($array, $left, $pivot - 1);
}
示例8: quicksort
function quicksort(array &$array, $start, $end, $value)
{
if ($start >= $end) {
return;
}
$pivotStartIndex = pickPivot($array, $start, $end);
$pivotNewIndex = partition($array, $start, $end, $pivotStartIndex, $value);
quicksort($array, $start, $pivotNewIndex - 1, $value);
quicksort($array, $pivotNewIndex + 1, $end, $value);
}
示例9: testLargeArraysOfRandomNumbersAreProperlySorted
/**
* @dataProvider pullData
*/
public function testLargeArraysOfRandomNumbersAreProperlySorted($unsortedArray)
{
$correctSorted = $unsortedArray;
$arrayToSort = $unsortedArray;
quicksort($arrayToSort, 0, count($arrayToSort) - 1, 'value');
usort($correctSorted, function ($a, $b) {
return $a['value'] - $b['value'];
});
$this->assertEquals($correctSorted, $arrayToSort);
}
示例10: quicksort
function quicksort($vetor)
{
if (count($vetor) <= 1) {
return $vetor;
}
$chave = array_shift($vetor);
return array_merge(quicksort(array_filter($vetor, function ($valor) use($chave) {
return $valor < $chave;
})), array($chave), quicksort($higher = array_filter($vetor, function ($valor) use($chave) {
return $valor >= $chave;
})));
}
示例11: quicksort
function quicksort(&$arr, $st, $end)
{
if ($st == $end) {
return;
}
$sep = $st;
for ($i = $st + 1; $i < $end; $i++) {
if ($arr[$i] < $arr[$st]) {
swap($arr, ++$sep, $i);
}
}
swap($arr, $st, $sep);
quicksort($arr, $st, $sep);
quicksort($arr, $sep + 1, $end);
}
示例12: getGachaSortByPriorityLevel
function getGachaSortByPriorityLevel(array $gachaObjectArray)
{
if (count($gachaObjectArray) == 0) {
return array();
}
$pivot = $gachaObjectArray[0];
$left = $right = array();
for ($i = 1; $i < count($gachaObjectArray); $i++) {
if ($gachaObjectArray[$i]->getGachaBasePriority() < $pivot->getGachaBasePriority()) {
$left[] = $gachaObjectArray[$i];
} else {
$right[] = $gachaObjectArray[$i];
}
}
return array_merge(quicksort($left), array($pivot), quicksort($right));
}
示例13: quicksort
function quicksort($seq)
{
if (!count($seq)) {
return $seq;
}
$k = $seq[0];
$x = $y = array();
for ($i = count($seq); --$i;) {
if ($seq[$i] <= $k) {
$x[] = $seq[$i];
} else {
$y[] = $seq[$i];
}
}
return array_merge(quicksort($x), array($k), quicksort($y));
}
示例14: quickSort
function quickSort($arr)
{
$loe = $gt = array();
if (count($arr) < 2) {
return $arr;
}
$p_key = key($arr);
$p = array_shift($arr);
foreach ($arr as $val) {
if ($val <= $p) {
$loe[] = $val;
} elseif ($val > $p) {
$gt[] = $val;
}
}
return array_merge(quicksort($loe), array($p_key => $p), quicksort($gt));
}
示例15: quicksort
function quicksort($input)
{
if (empty($input)) {
return $input;
}
$pivot = $input[0];
$l = $r = array();
for ($i = 1; $i < count($input); $i++) {
if ($input[$i] < $pivot) {
$l[] = $input[$i];
} else {
$r[] = $input[$i];
}
}
$merged = array_merge(quicksort($l), array($pivot), quicksort($r));
if (count($merged) > 1) {
echo implode(' ', $merged), "\n";
}
return $merged;
}