logo CodeStepByStep logo


Language/Type: PHP Stack Queue collections

Write a function named reorder that accepts as a parameter an array queue of integers that are already sorted by absolute value, and modifies it so that the integers are sorted normally.

For example, if a queue variable named $q stores the following elements:

front [1, -2, 4, 5, -7, -9, -12, 28, -34] back

Then the call of reorder($q) should modify it to store the following values:

front [-34, -12, -9, -7, -2, 1, 4, 5, 28] back

Constraints: You may use a single stack as auxiliary storage.

A note about references in PHP: In order to write a function passes a parameter as reference (thus modifying its state), you'll need to prepend "&" to the variable declaration in the function header. For example, a function foo that modifies the state of an array parameter may be defined as:

function foo(&$arr) { ... }
Function: Write a PHP function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.