HOME       >>       Programming

Bubble Sort Correct?


ianbrind

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

ragav.bpl

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

Nice program mate
but how to run it mate ........


VIEW DESKTOP VERSION REGISTERGET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.