Radix Sort

Radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value.



									function RadixSort(&$data, $count) {
	for ($shift = 31; $shift > -1; $shift--)
	{
		$j = 0;

		for ($i = 0; $i < $count; $i++)
		{
			$move = ($data[$i] << $shift) >= 0;

			if ($shift == 0 ? !$move : $move)
				$data[$i - $j] = $data[$i];
			else
				$temp[$j++] = $data[$i];
		}

		for ($i = 0; $i < $j; $i++)
		{
			$data[($count - $j) + $i] = $temp[$i];
		}
	}

	$temp = null;
}
								


Example

									$data = array(-1, 25, -58964, 8547, -119, 0, 78596);
RadixSort($data, 7);
								


Output

									-58964
-119
-1
0
25
8547
78596