BrianPeters Posted June 9, 2011 Share Posted June 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/238857-bubble-sort-algorithm-help/ Share on other sites More sharing options...
TeNDoLLA Posted June 9, 2011 Share Posted June 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/238857-bubble-sort-algorithm-help/#findComment-1227338 Share on other sites More sharing options...
Limetree Posted June 9, 2011 Share Posted June 9, 2011 } } ?> </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. Quote Link to comment https://forums.phpfreaks.com/topic/238857-bubble-sort-algorithm-help/#findComment-1227346 Share on other sites More sharing options...
TeNDoLLA Posted June 9, 2011 Share Posted June 9, 2011 } } ?> </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. Quote Link to comment https://forums.phpfreaks.com/topic/238857-bubble-sort-algorithm-help/#findComment-1227348 Share on other sites More sharing options...
BrianPeters Posted June 9, 2011 Author Share Posted June 9, 2011 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) Quote Link to comment https://forums.phpfreaks.com/topic/238857-bubble-sort-algorithm-help/#findComment-1227622 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.