RE: [PHP] Ok, I know this may sound like a stupid question...
| From: | Les Neste | Date: | Tue, 05 Dec 2000 21:37:34 +0000 |
| Subject: | RE: [PHP] Ok, I know this may sound like a stupid question... | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-28768@lists.php.net to get a copy of this message | ||
I may be missing something, but a casual analysis makes me think if you
don't use an array, it seems like you're going to end up with code that
uses if-elsif-else to distinguish between the specific cases, of which
there are 4*3*2, meaning you'll have 24 different if clauses.
You may be able to cut this down if you don't have 24 different outcomes --
in other words, if you only have to choose between doing 3 things based on
the resulting sort order, you can cut down on the number of tests.
My suggestion would be to bite the bullet, write a PHP function that
implements a bubble-sort on an array of size n, use it with your data, post
the function on zend.com, and then you can be a hero to all the PHP
programmers who come after you trying to solve the same problem.
Something like this:
function bubblesort( $ary )
Given a 1-dimensional array of arbitrary length, applies the bubble-sort
algorithm to the array and returns a new array containing the now-sorted
elements of the original array. Elements are sorted in numerical
increasing order, with the lowest-sort-order element in array position 0.
Returns true if successful, else false if an error condition was encountered.
You get the idea. HTH.
At 03:31 PM 12/5/2000 -0600, Jeff Demel wrote:
>Oops. My example uses an array, and you specifically said that was not the
>way you wanted to go.
>
>May I ask why?
>
>-Jeff
>
>-----Original Message-----
>From: Jeff Demel [mailto:jdlists@iglue.com]
>Sent: Tuesday, December 05, 2000 3:29 PM
>To: Boget, Chris; PHP (E-mail)
>Subject: RE: [PHP] Ok, I know this may sound like a stupid question...
>
>
>At the risk of bringing down flame-hellfire on my head, you could use a
>bubble sort. With so few variables to sort through, it should work great.
>
>Here's a JavaScript example:
>http://javascript.internet.com/calculators/bubble-sort.html
>
>-Jeff
>
>-----Original Message-----
>From: Boget, Chris [mailto:Chris.Boget@wild.net]
>Sent: Tuesday, December 05, 2000 3:12 PM
>To: PHP (E-mail)
>Subject: [PHP] Ok, I know this may sound like a stupid question...
>
>
>but I've been trying to find *simple* logic to do
>this and I cannot seem to wrap my head around
>it...
>
>I have 4 variables that are dynamically assigned
>numerical values. Let's say, after all the dynamic
>assignement is completed, I end up with:
>
>$this = 13;
>$that = 9;
>$other = 27;
>$bob = 2;
>
>What is the simplest way to figure out what order
>those variables should go in ascending order....
>Without putting the above 4 variables in an array...
>
>I've got some code, but it's soooo long. Longer than
>it should be, I'm sure.
>
>Thanks for any help you can provide!
>
>Chris
>
>
>--
>PHP General Mailing List (http://www.php.net/)
>To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
>For additional commands, e-mail: php-general-help@lists.php.net
>To contact the list administrators, e-mail: php-list-admin@lists.php.net
>
>
>
>--
>PHP General Mailing List (http://www.php.net/)
>To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
>For additional commands, e-mail: php-general-help@lists.php.net
>To contact the list administrators, e-mail: php-list-admin@lists.php.net
>
>
......................................................................
Les Neste
Cellphone 678-778-0382
Web http://www.lesneste.com