bubble sort crashing apache

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I've implemented my own bubble sort function that is crashing apache.
Can somebody either help me out with the bubble sort or help me figure
out a way to solve my problem with php's built in sorting functions?

This is my problem. I'm writing my own class for displaying html tables.
Right now, the only thing this class really abstracts is the sorting of
the table rows by any column ascending or descending. My table class
accepts an array of $row objects. Each $row object itself contains an
array of $cell objects. I've included the function I'm using to sort the
rows at the end of the email.

The problem I'm seeing is that after about the 3rd sort on a particular
column, apache crashes. Maybe bubble sort isn't the best sorting
algorithm, since when the user switches from ascending to descending
sort order on the same column or vice versa, I get the worst case
scenario for a bubble sort (the data is in reverse order). 

Any ideas? Thanks!!



private function sort() {
	$col_name = $this->order_by;
	
	/*
	** Get the column number of the column we're
	** ordering by.
	*/
	for ($i = 0; $i < count($this->col_names); $i++) {
		if (strtoupper($col_name) ==
strtoupper($this->col_names[$i])) {
			$col_num = $i;
			break;
		}				
	}
	
	/*
	** bubble sort
	*/	
	for ($n = 0; $n < count($this->contents); $n++) {
		$sorted = true;
		
		for ($y = 0; $y < count($this->contents) - 1; $y++) {
			$cell_arr_this =
$this->contents[$y]->get_cell_arr();
			$cell_arr_next = $this->contents[$y +
1]->get_cell_arr();
			
			if
($this->out_of_order($cell_arr_this[$col_num],$cell_arr_next[$col_num]))
{
				$tmp_row = $this->contents[$y];
				$this->contents[$y] = $this->contents[$y
+ 1];
				$this->contents[$y + 1] = $tmp_row;
				
				$sorted = false;
			}
		}
		
		if ($sorted) break;
	}
}

function out_of_order($val1, $val2) {
	if ($this->order_dir == "desc") {
		if ($val2 > $val1)
			return true;
		else 
			return false;
	} else {
		if ($val1 > $val2)
			return true;
		else 
			return false;
	}
}

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux