コード例 #1
0
/**
 * Assuming elements distribution is uniform and in the range 1 - 100 (see line
 * #34 to adjust the range property).
 *
 * @param reference &$elements Reference to array elements.
 * @return void $elements already sorted in place.
 */
function bucket_sort(&$elements)
{
    $n = sizeof($elements);
    $buckets = array();
    // Initialize the buckets.
    for ($i = 0; $i < $n; $i++) {
        $buckets[$i] = array();
    }
    // Put each element into matched bucket.
    foreach ($elements as $el) {
        array_push($buckets[ceil($el / 10)], $el);
    }
    // Sort elements in each bucket using insertion sort.
    $j = 0;
    for ($i = 0; $i < $n; $i++) {
        // sort only non-empty bucket
        if (!empty($buckets[$i])) {
            insertion_sort($buckets[$i]);
            // Move sorted elements in the bucket into original array.
            foreach ($buckets[$i] as $el) {
                $elements[$j++] = $el;
            }
        }
    }
}
コード例 #2
0
function insertion_sort($arr)
{
    $time_start = microtime(true);
    for ($i = 0; $i < count($arr) - 1; $i++) {
        if ($arr[$i] >= $arr[$i + 1]) {
            // if the value of the current pointer location is greater than the one to the right of it, then begin the sorting
            $c_val = $arr[$i + 1];
            //pull the lower value out and set up the sorting algorithm
            $x = $i;
            //Set a new variable to use for the array index so we don't lose our place overall
            while ($x > -1 && $c_val < $arr[$x]) {
                // move items in the array to the right as long as they are greater than the current value
                $arr[$x + 1] = $arr[$x];
                //move items in the array to the right
                $x--;
                //only decrement x if it is greater than 0
            }
            $arr[$x + 1] = $c_val;
            //after sorting, and we are in the right position to insert the value, set the value.
        }
    }
    var_dump($arr);
    $time_stop = microtime(true);
    echo $time_stop - $time_start;
}
$arr = [];
for ($i = 1; $i <= 10000; $i++) {
    $arr[] = rand(1, 10000);
}
insertion_sort($arr);
コード例 #3
0
{
    $time_strat = microtime(true);
    for ($i = 0; $i < count($arr) - 1; $i++) {
        // select a part of array for comparing;
        for ($j = $i; $j >= 0; $j--) {
            //compare every element in the comparing array from right to left;
            $comparing_element = $j + 1;
            //set the comparing element
            if ($arr[$comparing_element] < $arr[$j]) {
                swap($comparing_element, $j, $arr);
                //swaping the two elements;
            } else {
                break;
                //if the comparing element is equal or greater than the element before it, then end the comparing loop;
            }
        }
    }
    $time_end = microtime(true);
    $time = $time_end - $time_strat;
    echo "<span style = 'color:red; font-weight:bold;'>This insertion sort took: " . $time . " second(s).</span>";
    var_dump($arr);
}
$numbers = array();
for ($i = 0; $i < 100; $i++) {
    $numbers[] = rand(1, 10000);
}
echo "<span style = 'font-size:20; font-weight:bold;'>The original array:<br></span>";
var_dump($numbers);
echo "<span style = 'font-size:20; font-weight:bold;'>The sorted array:<br></span>";
insertion_sort($numbers);
コード例 #4
0
<?php

/**
 * Sorting array by insertion algorithm
 *
 * @package bubble_sort
 * @author Andrey Babaev <*****@*****.**>
 */
require_once '../../common/functions.php';
// Prepare test array
$a = array(4, 10, 21, 5, 12, 43, 0, -4, 12, 5, 6, 1, 3, 27, 11, 15);
// Print array before sorting
before_sort($a);
// Sort array
$a = insertion_sort($a);
// Print array after sorting
after_sort($a);
measure_exec_time('insertion_sort', 10000);
measure_exec_time('insertion_sort', 20000);
//measure_exec_time('insertion_sort', 30000);  // too long for wait
/**
 * Array sorting function
 * Use insertion algorythm for sorting
 *
 * @param array $a Array for sorting
 * @return array $a Sorting array
 */
function insertion_sort(array $a)
{
    $arr_length = count($a);
    for ($j = 1; $j < $arr_length; $j++) {
コード例 #5
0
ファイル: insertion_sort.php プロジェクト: jreyles/lamp-stack
}
function insertion_sort($arr)
{
    //Base case
    $first = $arr[0];
    $second = $arr[1];
    if ($arr[1] < $arr[0]) {
        swap($first, $second);
        $arr[0] = $first;
        $arr[1] = $second;
    }
    //Induction Hypothesis add 2
    foreach ($arr as $keys => $values) {
        //keys + 2 is current key
        for ($i = $keys + 2; $i < count($arr); $i++) {
            for ($j = $keys + 2; $j == 0; $j--) {
                if ($arr[$keys + 2] < $arr[$j]) {
                    // swap variables
                    //				swap($arr[$j],$arr[$keys+2]);
                    $temp = $arr[$j];
                    $arr[$j] = $arr[$keys + 2];
                    $arr[$keys + 2] = $temp;
                }
            }
        }
    }
    var_dump($arr);
}
$test_array = [6, 5, 3, 1, 8, 7, 2, 4];
insertion_sort($test_array);
コード例 #6
0
 public function testNoParameters()
 {
     $this->assertEmpty(insertion_sort([]));
 }
コード例 #7
0
ファイル: Advanced5.php プロジェクト: TEnders64/PHP_Advanced4
<?php

set_time_limit(60);
//var_dump(insertion_sort());
$time_start = microtime(true);
insertion_sort();
function insertion_sort()
{
    $list = make_array(10000);
    for ($i = 1; $i < count($list); $i++) {
        $val = $list[$i];
        $index = $i - 1;
        while ($index >= 0 && $val < $list[$index]) {
            $temp = $list[$index];
            $list[$index] = $val;
            $list[$index + 1] = $temp;
            $index--;
        }
    }
    return $list;
}
function make_array($size)
{
    $nums = array();
    for ($x = 0; $x < $size; $x++) {
        $nums[] = rand(0, 10000);
    }
    return $nums;
}
$time_end = microtime(true);
$time = $time_end - $time_start;
コード例 #8
0
 public function testNoParameters()
 {
     $this->assertEquals(array(), insertion_sort(array()));
 }
コード例 #9
0
			<p>
		<?php 
//Declare the Insertion Sort Function
function insertion_sort($array)
{
    $size = count($array);
    for ($i = 0; $i < $size; $i++) {
        $marker = $array[$i];
        $j = $i;
        while ($j > 0 && $array[$j - 1] > $marker) {
            $array[$j] = $array[$j - 1];
            $j -= 1;
        }
        $array[$j] = $marker;
    }
    return $array;
}
//Microtime function to set start time
$time_start = microtime(true);
//Printing the Array with commas
echo implode(", ", insertion_sort($sample_array));
//Stopping Microtime clock
$time_end = microtime(true);
$time = $time_end - $time_start;
//Printing result of Microtime function
echo "<br><br>Did sort in " . $time . " seconds\n";
?>
	</p>
		</div>
	</body>
</html>
コード例 #10
0
<?php

$array = [23, 43, 3, 12, 54, 65, 26, 32, 16, 43, 54, 59];
function insertion_sort($array)
{
    $n = count($array) - 1;
    // last index
    // j from 1 to $n
    for ($j = 1; $j <= $n; $j++) {
        // take out the $j;
        $out = $array[$j];
        // i from $j-1 to 0
        $i = $j - 1;
        while ($i >= 0 && $array[$i] > $out) {
            $array[$i + 1] = $array[$i];
            $i--;
        }
        $array[$i + 1] = $out;
    }
    return $array;
}
var_dump(insertion_sort($array));
コード例 #11
0
ファイル: index.php プロジェクト: sourabhpal/PHP
<?php

$sample = array();
for ($i = 0; $i < 100; $i++) {
    $sample[] = rand(0, 10000);
}
function insertion_sort($array)
{
    for ($i = 0; $i < count($array); $i++) {
        $value = $array[$i];
        $j = $i;
        while ($j > 0 && $array[$j - 1] > $value) {
            $array[$j] = $array[$j - 1];
            $j -= 1;
        }
        $array[$j] = $value;
    }
    echo microtime();
    return $array;
}
$sorted_array = insertion_sort($sample);
var_dump($sorted_array);
コード例 #12
0
<?php

function insertion_sort($array)
{
    $len = count($array);
    for ($i = 1; $i < $len; $i++) {
        $r = $array[$i];
        $j = $i;
        while ($j > 0 && $array[$j - 1] > $r) {
            $array[$j] = $array[$j - 1];
            $j--;
        }
        $array[$j] = $r;
    }
    return $array;
}
//demo
$x = array();
for ($i = 0; $i < 10000; $i++) {
    array_push($x, rand(1, 10000));
}
$start = microtime(true);
$y = insertion_sort($x);
$end = microtime(true);
$time_used = $end - $start;
echo "time used to sort: {$time_used} seconds<br>";
var_dump($x);
var_dump($y);
コード例 #13
0
                if ($array_to_sort[$i] < $s[$j]) {
                    $temp = $s[$j];
                    $s[$j] = $array_to_sort[$i];
                    $s[$j + 1] = $temp;
                    $flag = true;
                } else {
                    if (!$flag) {
                        array_push($s, $array_to_sort[$i]);
                        break;
                    }
                }
            }
        }
    }
}
r_print(insertion_sort($array_to_sort));
//////////////
//Merge Sort//
//////////////
function merge_sort($array_to_sort = '')
{
    if (count($array_to_sort) == 1) {
        return $array_to_sort;
    } else {
        $sp = count($array_to_sort) / 2;
        $sp = floor($sp);
        $left = array_slice($array_to_sort, 0, $sp);
        $right = array_slice($array_to_sort, $sp);
        $left = ms($left);
        $right = ms($right);
        $result = merge($left, $right);