Jump to content

Recommended Posts

Hey guys! I've been debugging this dang program for hours now and can't figure it out. If anybody wants to check it out i would appreciate it very much because i'm pulling out my hair! I'm sure its something simple. I'm working on the bubble algorithm to help teach myself php, and how to think through problems in code  ;)

 

The output is:

Sequence to unscramble: 54321

1

 

Not sure why it doesn't output the rest.

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Bubble Sort Algorithm</title>
</head>

<body>
<?php
    if (isset($_POST['numbers'])) {
    $string = $_POST['numbers'];
    $visible = true;

}
?>
    <h1>Welcome to the sequence de-scrambler</h1>
    <h2>Enter your number sequence to descramble</h2>
    
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
        <p>Enter combination: 
            <input type="text" name="numbers" id="numbers" />
            <input type="submit" value="Unscramble";
        </p>
    </form>
    <?php if ($visible) {
        echo "Sequence to unscramble: " . $string . "<br />";
        $unscrambled = unscramble_String($string);
        for ($i = 0; $i < strlen($string); $i++) {
            echo $unscrambled[$i] . " ";
        }     
    } ?>
    
</body>
</html>
<?php

function unscramble_String($input_string) {

for($i=0; $i < strlen($input_string); $i++) {
    $strarr = array($i => $input_string[$i]);
    
    }
    
    for($x=0; $x < strlen($input_string); $x++) {
        for($y = 0; $y < strlen($input_string); $y++) {
            if($strarr[$x] < $strarr[$y]) {
                $hold = $strarr[$x];
                $strarr[$x] = $strarr[$y];
                $strarr[$y] = $hold;
            }
        }
    }
        return $strarr;
}
?>

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/238857-bubble-sort-algorithm-help/
Share on other sites

What is this unscramble_String() function supposed to do exactly? Say for example to the input "54321" what should be the output? Can you explain a bit more.

 

Also try using var_dump($unscrambled); and see the result, is it what u expect it to be, and if it is not try to debug further why it is not.''

 

When looking closer at your for loops on this line

if($strarr[$x] < $strarr[$y])

With an input of "54321" I think you are actually comparing 5 < 5 , 4 < 4, 3 < 3, 2 < 2 and 1 < 1. Which just does not make sense.

 

      }   

    } ?>

   

</body>

</html>

<?php

 

The rest of the code is not executed for the simple reason, you have ended the HTML tag before the second php code starts. If you want the second PHP code to be executed, you must put the closing HTML tag at the end of second PHP code.

 

      }   

    } ?>

   

</body>

</html>

<?php

 

The rest of the code is not executed for the simple reason, you have ended the HTML tag before the second php code starts. If you want the second PHP code to be executed, you must put the closing HTML tag at the end of second PHP code.

 

Yes, the code will be executed no matter if he has closed or not the html tags. You can test this by adding some php echo or something in the end. Also its just the function declaration that is called before the HTML tags are closed which makes the function code run before the HTML is closed anyway.

Thanks I figured it out. For some reason the loop i was using to create an array (strarr) from my $input_string was not working.

(Thanks for the tip about the var_dump() function, worked great)

 

I was using

   

for($i=0; $i < strlen($input_string); $i++) {
    $strarr = array($i => $input_string[$i]);
      }

 

I found an awesome function to replace it,

str_split($input_string)

 

But what is wrong with what I was doing earlier to create the array? It makes sense to me. Would it make more sense like this:

 $strrarr[$i] = ($input_string[$i]); 

 

With an input of "54321" I think you are actually comparing 5 < 5 , 4 < 4, 3 < 3, 2 < 2 and 1 < 1. Which just does not make sense.

 

What i believe is happening is similar to combination cracking. First it is array[0] < array[0], but because $y is nested, it will increment while $x stays zero. Once the internal loop finishes, $x increments to 1.

 

 array[0] < array[0]
array[0] < array[1]
array[0] < array[2]
etc etc.... till end of string length
array[1] < array[0]
array[1] < array[1]
array[1] < array[2]

 

then it swaps the values and moves on. (sorting in order)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

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.