Exemplo n.º 1
0
function randomTest($N, $k)
{
    $es = array();
    $a = array();
    for ($i = 0; $i < $k; $i++) {
        $a[$i] = rand(0, $N);
    }
    $t1 = microtime(true);
    $q = new QuickSort($a);
    $q->sort();
    return array('time' => microtime(true) - $t1, 'swaps' => $q->getSwaps(), 'compares' => $q->getCompares());
}
 function reorder_data(&$array, $sort_type)
 {
     $sort_controller = new QuickSort(SortType::make_sort_type($sort_type));
     $sort_controller->sort($array);
 }
Exemplo n.º 3
0
 function testSortWhenManyRepeatedValuesInInput()
 {
     $sizes = array(30, 100, 300);
     $distinctValues = 4;
     foreach ($sizes as $size) {
         $a = array();
         $countOfInputArrayValues = array();
         for ($i = 0; $i < $distinctValues; $i++) {
             $countOfInputArrayValues[$i] = 0;
         }
         for ($i = 0; $i < $size; $i++) {
             $a[$i] = $i % $distinctValues;
             $countOfInputArrayValues[$i % $distinctValues] += 1;
         }
         $q = new QuickSort($a);
         $sorted = $q->sort();
         // Check ascending and count values
         $countOfOutputArrayValues = array();
         for ($i = 0; $i < $distinctValues; $i++) {
             $countOfOutputArrayValues[$i] = 0;
         }
         for ($i = 0; $i < $size; $i++) {
             if ($i > 0) {
                 $this->assertLessThanOrEqual($sorted[$i], $sorted[$i - 1], "Array is not descreasing");
             }
             $countOfOutputArrayValues[$sorted[$i]] += 1;
         }
         for ($i = 0; $i < $distinctValues; $i++) {
             $this->assertEquals($countOfOutputArrayValues[$i], $countOfInputArrayValues[$i]);
         }
     }
 }
Exemplo n.º 4
0
<?php

include dirname(__FILE__) . '/../src/autoload.php';
$a = array(20, 10, 50, 90, 30, 70, 60, 40, 80);
print_r($a);
$q = new QuickSort($a);
$a = $q->sort();
print_r($a);