Jump to content
xisto Community
• Sign Up

# 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 (see edit history)

#### Share this 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 ........

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now

×

• #### Activity

• Leaderboard
×
• 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.