Jump to content
Xisto Corporation
ianbrind

Bubble Sort Correct?

Recommended Posts

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
Edited by ianbrind

Share this post


Link to post
Share on other sites

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 ........ :P:P

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.