Exemplo n.º 1
0
        //put array_d into module, count
        for ($i = 0; $i < $len; $i++) {
            $array_module[$array_d[$i]]++;
        }
        //increment module array
        for ($i = 1; $i < 10; $i++) {
            $array_module[$i] += $array_module[$i - 1];
        }
        //move element from array to array_tmp
        for ($i = $len - 1; $i >= 0; $i--) {
            $d = $array_d[$i];
            $array_module[$d]--;
            $array_tmp[$array_module[$d]] = $array[$i];
        }
        $array = $array_tmp;
        $k = $k * 10;
    }
    return $array_tmp;
}
//demo
$x = array();
for ($i = 0; $i < 100; $i++) {
    array_push($x, rand(1, 1000));
}
$start = microtime(true);
$y = radix_sort($x);
$end = microtime(true);
$time_used = $end - $start;
echo "time used to sort: {$time_used} seconds<br>";
var_dump($x);
var_dump($y);
        }
        array_push($queues[$el % 10], $el);
    }
    // Queues are dequeued back into original elements.
    $i = 0;
    foreach ($queues as $key => $q) {
        while (!empty($queues[$key])) {
            $elements[$i++] = array_shift($queues[$key]);
        }
    }
    // Remaining iterations are determined based on longest digits element.
    $it = strlen($longest) - 1;
    $d = 10;
    while ($it--) {
        foreach ($elements as $el) {
            array_push($queues[floor($el / $d) % 10], $el);
        }
        $i = 0;
        foreach ($queues as $key => $q) {
            while (!empty($queues[$key])) {
                $elements[$i++] = array_shift($queues[$key]);
            }
        }
        $d *= 10;
    }
}
// Example usage:
$a = array(170, 45, 75, 90, 802, 24, 2, 66);
var_dump($a);
radix_sort($a);
var_dump($a);
Exemplo n.º 3
0
		printf("Pass #%d\n", $ix);

		$store = array();
		
		for($i = 0; $i < count($a); $i++){
			$digit = digit_at($a[$i], $ix, $max_digits);
			$store[$digit][] = $a[$i];
		}
		
	//	print_r($store);
		
		$a = array();
		
		for($i = 0; $i <= 9; $i++)
			if (@$store[$i])
				foreach($store[$i] as $x)
					$a[] = $x;
				
		print_r($a);
	}

	return $a;
}



$a = array(324,23,2,35,6,333,224,6555,23,2,4,78);

print_r(radix_sort($a));

Exemplo n.º 4
0
        $num = rand(0, 10000);
        array_push($arr, $num);
    }
    return $arr;
}
function radix_sort($array)
{
    $most_digits = 0;
    foreach ($array as $value) {
        $length = strlen($value);
        if ($length > $most_digits) {
            $most_digits = $length;
        }
    }
    for ($i = 0; $i < $most_digits; $i++) {
        $bucket = array(array(), array(), array(), array(), array(), array(), array(), array(), array(), array());
        for ($j = 0; $j < count($array); $j++) {
            $bucket[floor($array[$j] / pow(10, $i)) % 10][] = $array[$j];
        }
        $array = array();
        for ($k = 0; $k < count($bucket); $k++) {
            foreach ($bucket[$k] as $value) {
                $array[] = $value;
            }
        }
    }
    var_dump($array);
}
$myArray = createArray();
radix_sort($myArray);
Exemplo n.º 5
0
<?php

define(MIN, 1);
define(MAX, 9);
$list = array(4, 3, 5, 9, 7, 2, 4, 1, 6, 5);
function radix_sort(&$input)
{
    $temp = array();
    $len = count($input);
    // initialize with 0s
    $temp = array_fill(MIN, MAX - MIN + 1, 0);
    foreach ($input as $key => $val) {
        $temp[$val]++;
    }
    $input = array();
    foreach ($temp as $key => $val) {
        if ($val == 1) {
            $input[] = $key;
        } else {
            while ($val--) {
                $input[] = $key;
            }
        }
    }
}
// 4, 3, 5, 9, 7, 2, 4, 1, 6, 5
echo implode(' ', $list), "\n";
radix_sort($list);
// 1, 2, 3, 4, 5, 5, 6, 7, 8, 9
echo implode(' ', $list), "\n";