Page 1 of 1

Sorting an array

Posted: Sat Feb 05, 2005 6:58 am
by [ICR]
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:

Code: Select all

for ($x = 1; $x < 8; $x=$x+2)
&#123;
	for ($y = 1; $y < 8; $y=$y+2)
	&#123;
		if ($board&#1111;$x]&#1111;$y])
		&#123;
			while ($board&#1111;$x1]&#1111;$y1])
			&#123;
				$x1++;
				if ($x1 > 7)
				&#123;
					$y1++;
					$x1=1;
				&#125;
			&#125;
			$board&#1111;$x1]&#1111;$y1] = $board&#1111;$x]&#1111;$y];
			$board&#1111;$x]&#1111;$y] = 0;
		&#125;
	&#125;
&#125;
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.

Posted: Sat Feb 05, 2005 7:40 am
by feyd
your algorithm doesn't have stops for infinite loops. It's quite possible an infinite loop is happening.