<?php
// A PHP program to put positive numbers
// at even indexes (0, 2, 4,..) and negative
// numbers at odd indexes (1, 3, 5, ..)
// The main function that rearranges elements
// of given array. It puts positive elements
// at even indexes (0, 2, ..) and negative
// numbers at odd indexes (1, 3, ..).
function rearrange(&$arr, $n)
{
// The following few lines are similar
// to partition process of QuickSort.
// The idea is to consider 0 as pivot
// and divide the array around it.
$i = -1;
for ($j = 0; $j < $n; $j++)
{
if ($arr[$j] < 0)
{
$i++;
swap($arr[$i], $arr[$j]);
}
}
// Now all positive numbers are at end and
// negative numbers at the beginning of array.
// Initialize indexes for starting point of
// positive and negative numbers to be swapped
$pos = $i + 1;
$neg = 0;
// Increment the negative index by 2 and
// positive index by 1, i.e., swap every
// alternate negative number with next
// positive number
while ($pos < $n && $neg < $pos &&
$arr[$neg] < 0)
{
swap($arr[$neg], $arr[$pos]);
$pos++;
$neg += 2;
}
}
// A utility function to swap two elements
function swap(&$a, &$b)
{
$temp = $a;
$a = $b;
$b = $temp;
}
// A utility function to print an array
function printArray(&$arr, $n)
{
for ($i = 0; $i < $n; $i++)
echo " " . $arr[$i] . " ";
}
// Driver Code
$arr = array(-1, 2, -3, 4, 5, 6, -7, 8, 9);
$n = count($arr);
rearrange($arr, $n);
printArray($arr, $n);
// This code is contributed
// by rathbhupendra
?>