Quick Sort Recursive

Quick sort is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order.



									function Partition(&$data, $left, $right) {
	$pivot = $data[$right];
	$temp;
	$i = $left;

	for ($j = $left; $j < $right; $j++)
	{
		if ($data[$j] <= $pivot)
		{
			$temp = $data[$j];
			$data[$j] = $data[$i];
			$data[$i] = $temp;
			$i++;
		}
	}

	$data[$right] = $data[$i];
	$data[$i] = $pivot;

	return $i;
}

function QuickSortRecursive(&$data, $left, $right) {
	if ($left < $right)
	{
		$q = Partition($data, $left, $right);
		QuickSortRecursive($data, $left, $q - 1);
		QuickSortRecursive($data, $q + 1, $right);
	}
}
								


Example

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


Output

									-58964
-119
-1
0
25
8547
78596