Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Counting "program" Issues

    So i'm attempting to create a script that is simply a replica of the "sort" pre-defined php function. Basically it takes a user input array and sorts it (So far limited numerically). I have a problem though. I'm hoping someone can point out a simple issue with the program, whatever it may be. I'm probably somewhat tired and in that mindset where nothing I do can help it anymore.
    BTW: It is supposed to sort from highest number to lowest number, I realise it doesn't go through the array for each element or whatever (Main point, it won't be fully sorted, technically it just replaces neighbouring values)

    PHP Code:
    <?php 
    // sorter function kinda sorts provided array through foreach loop
    function sorter(&$array)
    {    
        for (
    $i 0$u =1;  $u>=count($array)-1$i++, $u++)
        {
            if (
    $array[$i] < $array[$u])
            {
                
    $temp $array[i];
                
    $array[$i] = $array[$u];
                
    $array[$u] = $temp;
                
                
            } else {
            
    print_r ($array);
            }
        }
    }    
    //Ex. array is set, than called by the "sorter" function
    $set = array (0=>712158206);
    sorter($set);    
    echo 
    count($set);
    print_r ($set);
    From my limited knowledge of php, here's what I would assume it does:
    $i starts at 0, and $u at 1. these are meant to locate the first(0) and second(1) elements of the array respectively. Both are incremented each iteration of the loop. It checks if the first element is lesser to the second element, and if it is performs the swap. It will keep going up the elements untill it cannot anymore, then it should head to the else statement of the construct where it prints the information.

    Say on the first run of the loop the if statement is true, and it executes the statements. Does it directly go to the next iteration? That's just another question.


    I realise I could be doing this with a switch and whatnot(a million easier ways to sort an array), but my main focus for this little project is to maintain as much structure as possible at the moment. I want to know if in the future I would be able to make a program that could have the same function, but be done with a different style of code. Anway, all help is greatly appreciated



    Edit: WAAAAIT. I just realised, if I'm not printing anything each iteration, than I'm constantly re-declaring variables without actually showing them? What if I just pushed the bigger number onto a separate array on each iteration of the loop? That way it would correctly organize the information!
    Last edited by Inigoesdr; 07-06-2012 at 01:45 AM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,511
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    Is this homework? we don't do homework ... see codingforums posting rules.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That terminating condition is incorrect. That should be going to $u < count($array). As is, it will return false on the evaluation causing the loop to never occur.

    Since the sort is incomplete (ie, you're this far why bother leaving it as a partial sort?), you may want to read over the different types of sorts here: http://en.wikipedia.org/wiki/Sorting_algorithm
    Heap sort, shell sort and quick sort are my favorites. PHP's internal engine uses a quick sort.

  • #4
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    Is this homework? we don't do homework ... see codingforums posting rules.
    No it's not homework sorry. I come from a small town in SK where no coding programs exist, plus, I'm on summer holidays.

    Quote Originally Posted by Fou-Lu View Post
    That terminating condition is incorrect. That should be going to $u < count($array). As is, it will return false on the evaluation causing the loop to never occur.

    Since the sort is incomplete (ie, you're this far why bother leaving it as a partial sort?), you may want to read over the different types of sorts here: http://en.wikipedia.org/wiki/Sorting_algorithm
    Heap sort, shell sort and quick sort are my favorites. PHP's internal engine uses a quick sort.
    This literally blew my mind. So now that I've changed it and it works, correct me if I'm wrong: It would continuously iterate because the statement was >= therefore it could go greater?

    Doesn't the loop end once the condition evaluates the false? Wait a second.....

    Actually won't it only iterate when that condition is true? therefore $u>= would start out false so it wouldn't be able to iterate. That actually makes quite a bit of sense. Thanks a lot man!

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yep, you got it right. It'll never iterate this particular array.
    The second condition is the continuation condition within the for. So it says, so long as this condition evaluates true, go again. Since you specified $u>=count($array)-1, the $u represents 1, so the only time it will attempt to iterate is if the number of items in the array - 1 <= 1. So if you gave it an array with 0, 1, or 2 items in it, it will infinitely loop.

    Edit:
    Ah good o'l Saskatchewan. Hopefully one of them recent tornado's passed you over. Lots of TD's this year already, so it looks like it'll be an exciting summer for weather!
    Last edited by Fou-Lu; 07-06-2012 at 12:17 AM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •