This is related to PHP's array.
An array has number of elements. All elements are integers and unique, which means there is no repetitive integers.
(e.g.) $foo = array(7, 5, 9, 13, 2, 8);
You have to sort the array, provided:
An array has number of elements. All elements are integers and unique, which means there is no repetitive integers.
(e.g.) $foo = array(7, 5, 9, 13, 2, 8);
You have to sort the array, provided:
- You should scan the elements only once.
- You're not allowed to compare the elements when sorting. (i.e., you're not supposed to use any comparison operators)
- Sorted resultant array may not be the source array.
Comments
reset($foo);
$bar = array();
$barcount = 0;
while ($foo <> array()) {
$bartemp = min($foo);
$bar[$barcount] = $bartemp;
unset($foo[array_search($bartemp, $foo)]);
$barcount++;
}
print_r($foo); //empty
echo "\n";
print_r($bar); //sorted!
?>
Result:
$foo is Array
(
)
$bar is Array
(
[0] => 2
[1] => 5
[2] => 7
[3] => 8
[4] => 9
[5] => 13
)
sort($bar);
reset($bar);
print_r($bar);
For comment#2:Are you kidding;)