Shell Sort

Shell sort (a.k.a Shell's Method and Diminishing Increment Sort) is an in-place comparison sort algorithm. The method starts by sorting pairs of elements far apart from each other, then progressively reducing the gap between elements to be compared. Starting with far apart elements can move some out-of-place elements into position faster than a simple nearest neighbor exchange.



									function ShellSort(&$data, $count) {
	$hSort = 1;

	while ($hSort < floor($count / 3))
		$hSort = (3 * $hSort) + 1;

	while ($hSort >= 1)
	{
		for ($i = $hSort; $i < $count; $i++)
		{
			for ($a = $i; $a >= $hSort && ($data[$a] < $data[$a - $hSort]); $a -= $hSort)
			{
				$data[$a] ^= $data[$a - $hSort];
				$data[$a - $hSort] ^= $data[$a];
				$data[$a] ^= $data[$a - $hSort];
			}
		}

		$hSort = floor($hSort / 3);
	}
}
								


Example

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


Output

									-58964
-119
-1
0
25
8547
78596