Sorting an array
Posted: Sat Feb 05, 2005 6:58 am
I have a multidimentional array of 7x7 filled with random values from 0 to 12. I want to go through ever other item in every other column (so 1,1 1,3 1,5 1,7 3,1 3,3 3,5 3,7 5,1 5,3 etc.) and swap it's value for a cell in the array which contains a 0.
I wrote an algorithm that *should* do it (may be bugs) but it timesout, and i would like a quicker way of doing it:
I know the code is bad, but I'm tired and my head hurts from too much coding. Any help would be much apreciated.
[edit] I know I can start the array on 0, but this makes it easier to read, as I write it to a database, starting with row 1.
I wrote an algorithm that *should* do it (may be bugs) but it timesout, and i would like a quicker way of doing it:
Code: Select all
for ($x = 1; $x < 8; $x=$x+2)
{
for ($y = 1; $y < 8; $y=$y+2)
{
if ($boardї$x]ї$y])
{
while ($boardї$x1]ї$y1])
{
$x1++;
if ($x1 > 7)
{
$y1++;
$x1=1;
}
}
$boardї$x1]ї$y1] = $boardї$x]ї$y];
$boardї$x]ї$y] = 0;
}
}
}[edit] I know I can start the array on 0, but this makes it easier to read, as I write it to a database, starting with row 1.