Hi, Today I went about creating an ideal bubble sort algorithm. I came up with this:
$this->flag = true; if($this->p==true) $this->returned .= ''; for($i = 1; $i < $num; ++$i){ if($this->p==true){ if($i==$num-1) $c = 'style="border linenums:0'>Class Bubble{ function Sort($array, $num, $process = false){ $this->p = $process; $this->returned = ''; $this->flag = true; if($this->p==true) $this->returned .= ''; for($i = 1; $i < $num; ++$i){ if($this->p==true){ if($i==$num-1) $c = 'style="border:eeee88; background:#ffff88;"'; else $c=''; $this->returned .= 'AAAAAA'; } for($x = 1; $x < $num; ++$x){ if($array[$i] < $array[$x]){ $hold = $array[$i]; $array[$i] = $array[$x]; $array[$x] = $hold; } if($this->p==true) $this->returned .= 'AAA'; } } if($this->p==true) $this->returned .= 'AAAAAABBBSort '.$i.''.$array[$x].''; if($this->p==true) return $this->returned; else return $array; }} $ns = $B->Sort($n, $n1, true); // $n is array (1,2,5,9,11,52,27), $n1 is array size echo $ns;
The output of $ns display the individual steps in the sorting algorithm but it doesn't look like a conventional bubble sort table. I think there may be a logic error in my for loops. (AAA/BBB are table tags) Any thoughts? Thanks