Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-05-2012, 05:48 AM   PM User | #1
Nippy
New to the CF scene

 
Join Date: Jul 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Nippy is an unknown quantity at this point
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..
Nippy is offline   Reply With Quote
Old 07-05-2012, 03:16 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Is this homework? we don't do homework ... see codingforums posting rules.
mlseim is offline   Reply With Quote
Old 07-05-2012, 03:48 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 07-05-2012, 11:58 PM   PM User | #4
Nippy
New to the CF scene

 
Join Date: Jul 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Nippy is an unknown quantity at this point
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!
Nippy is offline   Reply With Quote
Old 07-06-2012, 12:10 AM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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..
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
counting, issue, program

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:33 PM.


Advertisement
Log in to turn off these ads.